簡體   English   中英

Java Swing布局和菜單

[英]Java Swing Layouts and Menus

好的,所以我在使用網格布局以這種格式排列窗口時遇到麻煩,我想將每個標簽及其文本字段排列到單獨的一行,然后在底部有一個居中的按鈕。

  public class Main extends JFrame {

   //below is how i want to format the frame
        /*label textfield
          label textfield   
          label textfield   
              button*/

         JPanel panel = new JPanel();
        JLabel nameLabel = new JLabel("Name");
        JTextField nameText = new JTextField(15);
        JLabel addressLabel = new JLabel("Address");
        JTextField addressText = new JTextField(15);

        public Main(){
            setTitle("JSWING");
            setLayout(new GridLayout(3,2));
            setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
            setVisible(true);
            setSize(450,250);

         //setResizable(false);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

            panel.add(nameLabel);
            panel.add(nameText);
            panel.add(addressLabel);
            panel.add(addressText);


            add(panel);
        }
        public static void main(String[] args){
            Main mainFrame = new Main();
            //mainFrame.setVisible(true);
        }
    }
//setVisible(true); // don't do this until all components are added to the frame.
...
add(panel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
JButton button = new JButton("Button");
buttonPanel.add( button );
add(buttonPanel, BorderLayout.PAGE_END);
setVisible(true);

框架的默認布局管理器是BorderLayout。 因此,您可以在框架中添加多個面板。 一個面板包含您的標簽/文本字段,另一個面板包含您的按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM