Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JAVA中使用多布局管理器_Java_User Interface_Layout_Jbutton - Fatal编程技术网

在JAVA中使用多布局管理器

在JAVA中使用多布局管理器,java,user-interface,layout,jbutton,Java,User Interface,Layout,Jbutton,我想把按钮放在“898美食餐厅”的下方Jlabel。按钮的setLocation()不工作 public class MainMenu extends JPanel{ JLabel picLabel,title; JButton button; public MainMenu() throws IOException { JPanel panel = new JPanel(new BorderLayout()); BufferedImage my

我想把
按钮
放在“898美食餐厅”的下方
Jlabel
按钮的
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);
    }

}

每个JC组件(如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(); 
    }  
    }

改为尝试垂直框或Boxlayout。Boxlayout无法共享更改为
setLayout(新的Boxlayout(getContentPane(),Boxlayout.Y_轴))问题解决了。如何调整按钮和标签的位置?