【问题标题】:Use multiple layout manager in JAVA在 JAVA 中使用多个布局管理器
【发布时间】:2017-11-02 20:25:58
【问题描述】:

我想将button 放在“898 美食餐厅”Jlabel 下方。 buttonsetLocation() 不起作用。

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);
    }

}

【问题讨论】:

  • 尝试使用垂直 Box 或 Boxlayout。

标签: java user-interface layout jbutton


【解决方案1】:

每个 JComponent(例如 JPanel)一次只能有一个布局管理器。但是由于 JComponents 可以嵌套,因此您的 JFrame 中可以有不同的布局管理器。通常这就是您创建复杂布局的方式。

现在关于按钮放置的问题。 setLocation 不会做任何事情,因为您的按钮位于 JPanel 中,并且默认情况下它使用忽略 location 属性的 FlowLayout。第一步是将 buttonPanel 布局设置为 null。但这可能还不够,因为 buttonPanel 由另一个流布局定位,它将设置它的边界不在嵌套按钮的位置坐标内。 您始终可以通过将 JPanel 的背景设置为不同的颜色来查看 JPanel 边界。

我的建议是始终尝试使用布局管理器定位组件并避免绝对定位。

【讨论】:

    【解决方案2】:

    您可以使用 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
    • 改成setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));就解决了。
    • 如何调整按钮和 JLabel 的位置?
    猜你喜欢
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    相关资源
    最近更新 更多