2017-06-01 23 views
1

我想将button放置在“898 Food Restaurant”Jlabel以下。 setLocation()button不起作用。在JAVA中使用多个布局管理器

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

} 

enter image description here

+1

尝试垂直箱或BoxLayout的替代。 – rob

回答

0

可以替代的FrameLayout的使用BoxedLayout:

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(); 
    } 
    } 
+0

BoxLayout无法共享 –

+0

更改为'setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));'并解决。 –

+0

如何调整按钮和JLabel的位置? –

2

同时每个JComponent如JPanel中只能有一个布局管理器。但由于JComponents可以嵌套,因此可以在JFrame中使用不同的布局管理器。通常这就是你如何创建复杂的布局。

现在针对您关于按钮放置的问题。 setLocation不会执行任何操作,因为您的按钮位于JPanel中,默认情况下它使用忽略位置属性的FlowLayout。第一步是将buttonPanel布局设置为null。但是,这仍然可能是不够的,因为buttonPanel被另一个流布局定位,它将设置它的边界不在嵌套按钮的位置坐标内。 通过将背景设置为不同的颜色,您始终可以看到您的JPanel边界。

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