2017-06-01 59 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邊界。

我的建議是總是嘗試使用佈局管理器來定位組件,並避免絕對定位。