在 JAVA 中使用多个布局管理器
Use multiple layout manager in JAVA
我想把button
放在“898美食餐厅”Jlabel
下面。 button
的 setLocation()
不工作。
public class MainMenu extends JPanel{
JLabel picLabel,title;
JButton button;
public MainMenu() throws IOException
{
JPanel panel = new JPanel(new BorderLayout());
BufferedImage myPicture = ImageIO.read(new File("C:\Users\seng\workspace\FoodOrderingSystem\ramen-noodles.png"));
Image scaled = myPicture.getScaledInstance(170,170,Image.SCALE_SMOOTH);
picLabel = new JLabel(new ImageIcon(scaled));
title = new JLabel("898 Food Restaurant");
title.setFont(new Font("Serif",Font.ITALIC+Font.BOLD,18));
title.setForeground(Color.BLUE);
button = new JButton("Order Food Now >>");
button.setLocation(40,380);
button.setSize(40,80);
panel.add(picLabel,BorderLayout.CENTER);
panel.add(title,BorderLayout.SOUTH);
JPanel buttonPanel = new JPanel();
buttonPanel.add(button);
add(buttonPanel);
add(panel);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
OrderMainPage order = new OrderMainPage();
}
});
}
public static void main(String args[]) throws IOException
{
MainMenu main = new MainMenu();
JFrame frame = new JFrame();
frame.setTitle("898 Food Ordering System");
frame.add(main);
// frame.setSize(120,130);
frame.pack(); // size
frame.setLocationRelativeTo(null); // place frame in center
frame.setVisible(true);
}
}
每个 JComponent(例如 JPanel)一次只能有一个布局管理器。但由于 JComponents 可以嵌套,您可以在 JFrame 中使用不同的布局管理器。通常这就是创建复杂布局的方式。
现在回答您关于按钮放置的问题。 setLocation 不会执行任何操作,因为您的按钮位于 JPanel 中,并且默认情况下它使用忽略位置属性的 FlowLayout。第一步是将 buttonPanel 布局设置为 null。但这可能仍然不够,因为 buttonPanel 由另一个流布局定位,它将设置它的边界不在嵌套按钮的位置坐标内。
您始终可以通过将 JPanel 的背景设置为不同的颜色来查看它的边界。
我的建议是始终尝试使用布局管理器定位组件并避免绝对定位。
您可以使用 BoxedLayout 代替 FrameLayout:
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
class MainMenu extends Frame {
JLabel picLabel,title;
JButton button;
public MainMenu () {
JPanel panel = new JPanel(new BorderLayout());
try{
BufferedImage myPicture = ImageIO.read(new File("C:\Users\seng\workspace\FoodOrderingSystem\ramen-noodles.png"));
Image scaled = myPicture.getScaledInstance(170,170,Image.SCALE_SMOOTH);
picLabel = new JLabel(new ImageIcon(scaled));}catch(Exception e){}
title = new JLabel("898 Food Restaurant");
title.setFont(new Font("Serif",Font.ITALIC+Font.BOLD,18));
title.setForeground(Color.BLUE);
button = new JButton("Order Food Now >>");
panel.add(picLabel,BorderLayout.CENTER);
panel.add(title,BorderLayout.SOUTH);
JPanel buttonPanel = new JPanel();
buttonPanel.add(button);
add(panel);
add(buttonPanel);
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
setSize(400,400);
setVisible(true);
}
public static void main(String args[]){
MainMenu main = new MainMenu();
}
}
我想把button
放在“898美食餐厅”Jlabel
下面。 button
的 setLocation()
不工作。
public class MainMenu extends JPanel{
JLabel picLabel,title;
JButton button;
public MainMenu() throws IOException
{
JPanel panel = new JPanel(new BorderLayout());
BufferedImage myPicture = ImageIO.read(new File("C:\Users\seng\workspace\FoodOrderingSystem\ramen-noodles.png"));
Image scaled = myPicture.getScaledInstance(170,170,Image.SCALE_SMOOTH);
picLabel = new JLabel(new ImageIcon(scaled));
title = new JLabel("898 Food Restaurant");
title.setFont(new Font("Serif",Font.ITALIC+Font.BOLD,18));
title.setForeground(Color.BLUE);
button = new JButton("Order Food Now >>");
button.setLocation(40,380);
button.setSize(40,80);
panel.add(picLabel,BorderLayout.CENTER);
panel.add(title,BorderLayout.SOUTH);
JPanel buttonPanel = new JPanel();
buttonPanel.add(button);
add(buttonPanel);
add(panel);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
OrderMainPage order = new OrderMainPage();
}
});
}
public static void main(String args[]) throws IOException
{
MainMenu main = new MainMenu();
JFrame frame = new JFrame();
frame.setTitle("898 Food Ordering System");
frame.add(main);
// frame.setSize(120,130);
frame.pack(); // size
frame.setLocationRelativeTo(null); // place frame in center
frame.setVisible(true);
}
}
每个 JComponent(例如 JPanel)一次只能有一个布局管理器。但由于 JComponents 可以嵌套,您可以在 JFrame 中使用不同的布局管理器。通常这就是创建复杂布局的方式。
现在回答您关于按钮放置的问题。 setLocation 不会执行任何操作,因为您的按钮位于 JPanel 中,并且默认情况下它使用忽略位置属性的 FlowLayout。第一步是将 buttonPanel 布局设置为 null。但这可能仍然不够,因为 buttonPanel 由另一个流布局定位,它将设置它的边界不在嵌套按钮的位置坐标内。 您始终可以通过将 JPanel 的背景设置为不同的颜色来查看它的边界。
我的建议是始终尝试使用布局管理器定位组件并避免绝对定位。
您可以使用 BoxedLayout 代替 FrameLayout:
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
class MainMenu extends Frame {
JLabel picLabel,title;
JButton button;
public MainMenu () {
JPanel panel = new JPanel(new BorderLayout());
try{
BufferedImage myPicture = ImageIO.read(new File("C:\Users\seng\workspace\FoodOrderingSystem\ramen-noodles.png"));
Image scaled = myPicture.getScaledInstance(170,170,Image.SCALE_SMOOTH);
picLabel = new JLabel(new ImageIcon(scaled));}catch(Exception e){}
title = new JLabel("898 Food Restaurant");
title.setFont(new Font("Serif",Font.ITALIC+Font.BOLD,18));
title.setForeground(Color.BLUE);
button = new JButton("Order Food Now >>");
panel.add(picLabel,BorderLayout.CENTER);
panel.add(title,BorderLayout.SOUTH);
JPanel buttonPanel = new JPanel();
buttonPanel.add(button);
add(panel);
add(buttonPanel);
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
setSize(400,400);
setVisible(true);
}
public static void main(String args[]){
MainMenu main = new MainMenu();
}
}