多个面板彼此之下

3 java user-interface swing layout-manager gridbaglayout

是否有可能我有一个登录的顶部面板.然后在下面的中间和底部面板.我正在使用gridbaglayout,我已经分配了2个面板.一个面板具有与其链接的组件以用于登录详细信息,另一个面板具有链接到其的其他组件.但是,计划不会发生.香港专业教育学院尝试使用Box/Borderlayout来做到这一点,但后来我无法创建我想要的间距.

这是我想要的草图.

http://s16.postimage.org/dtefn48qd/16_11_2012_11_24_40.png

public class GUI extends JFrame{

    private JPanel myTopPL, myTopPL2;
    private JTextField myUsernameTF;
    private JTextField myPasswordTF;
    private JTextField myMessageTF;
    private JLabel myUsernameLBL;
    private JLabel myPasswordLBL;
    private JLabel myItemsLBL;
    private JTextArea myMainTA;
    private JButton myLoginBN;
    private JButton myConnectBN;
    private JButton mySendBN;
    private JComboBox myItemsCB;

    public GUI () {

        super("GUI ");

        setLayout(new GridBagLayout());
        myTopPL = new JPanel();
        myTopPL2 = new JPanel();
        myTopPL.setLayout(new GridBagLayout());
        myTopPL2.setLayout(new GridBagLayout());
        GridBagConstraints myTopPLGBC = new GridBagConstraints();
        myTopPLGBC.weightx = 1;
        myTopPLGBC.weighty = 1;
        GridBagConstraints myTopPLGBC2 = new GridBagConstraints();
        myTopPLGBC.weightx = 2;
        myTopPLGBC.weighty = 2;

        myUsernameLBL = new JLabel("Username: ");
        myUsernameLBL.setFont(new Font("Arial", Font.BOLD, 13));
        myTopPLGBC.gridx = 1;
        myTopPLGBC.gridy = 1;
        myTopPLGBC.insets = new Insets(5,5,5,5);
        myTopPL.add(myUsernameLBL, myTopPLGBC);

        myUsernameTF = new JTextField(10);
        myTopPLGBC.gridx = 2;
        myTopPLGBC.gridy = 1;
        myTopPLGBC.insets = new Insets(5,5,5,5);
        myTopPL.add(myUsernameTF,myTopPLGBC);

        myPasswordLBL = new JLabel("Password: ");
        myPasswordLBL.setFont(new Font("Arial", Font.BOLD, 13));
        myTopPLGBC.gridx = 3;
        myTopPLGBC.gridy = 1;
        myTopPLGBC.insets = new Insets(5,5,5,5);
        myTopPL.add(myPasswordLBL, myTopPLGBC);

        myPasswordTF = new JTextField(10);
        myTopPLGBC.gridx = 4;
        myTopPLGBC.gridy = 1;
        myTopPLGBC.insets = new Insets(5,5,5,5);
        myTopPL.add(myPasswordTF,myTopPLGBC);

        myLoginBN = new JButton("Login");
        myTopPLGBC.gridx = 5;
        myTopPLGBC.gridy = 1;
        myTopPLGBC.insets = new Insets(5,5,5,5);
        myTopPL.add(myLoginBN,myTopPLGBC);

        myItemsLBL = new JLabel("Items: ");
        myItemsLBL.setFont(new Font("Arial", Font.BOLD, 13));
        myTopPLGBC2.gridx = 1;
        myTopPLGBC2.gridy = 3;
        myTopPLGBC2.insets = new Insets(5,5,5,5);
        myTopPL2.add(myItemsLBL, myTopPLGBC2);

        myItemsCB = new JComboBox();
        myItemsCB.addItem("     Select an Item     ");
        myTopPLGBC2.gridx = 1;
        myTopPLGBC2.gridy = 4;
        myTopPLGBC2.insets = new Insets(5,5,5,5);
        myTopPL2.add(myItemsCB, myTopPLGBC2);

        myConnectBN = new JButton("Connect");
        myTopPLGBC2.gridx = 2;
        myTopPLGBC2.gridy = 4;
        myTopPLGBC2.insets = new Insets (5,5,5,5);
        myTopPL2.add(myConnectBN, myTopPLGBC2);

        GridBagConstraints GBC = new GridBagConstraints();
        GBC.anchor = GridBagConstraints.NORTHWEST;
        GBC.weightx = 1;
        GBC.weighty = 1; 
        this.add(myTopPL,GBC);  

        GridBagConstraints GBC2 = new GridBagConstraints();
        GBC2.anchor = GridBagConstraints.NORTHWEST;
        GBC2.weightx = 2;
        GBC2.weighty = 2; 
        this.add(myTopPL2,GBC2);    

    }

    public static void main(String[] args) {

        GUI GUI = new GUI ();
        GUI .setDefaultCloseOperation(RMIAuctionHouse.EXIT_ON_CLOSE);
        GUI .setSize(750,500);
        GUI .setVisible(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

Mad*_*mer 5

我将应用程序分解为各自的职责范围,并专注于创建支持它的UI元素.这将使您能够专注于应用程序的每个部分的需求,它还允许您有机会根据需要更改布局

在此输入图像描述

public class TestLayout15 {

    public static void main(String[] args) {
        new TestLayout15();
    }

    public TestLayout15() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class MainPane extends JPanel {

        private JTextField messageField;
        private JButton sendButton;

        public MainPane() {
            setBorder(new EmptyBorder(4, 4, 4, 4));
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(new LoginPane(), gbc);
            gbc.gridy++;
            add(new ConnectPane(), gbc);
            gbc.gridy++;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(new JScrollPane(new JTextArea(5, 20)), gbc);

            gbc.gridwidth = 1;

            messageField = new JTextField(10);
            sendButton = new JButton("Send");

            gbc.gridy++;
            gbc.weighty = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(messageField, gbc);
            gbc.gridx++;
            gbc.weightx = 0;
            add(sendButton, gbc);            
        }

    }

    public class ConnectPane extends JPanel {

        private JComboBox comboBox;
        private JButton connectButton;

        public ConnectPane() {
            comboBox = new JComboBox();
            connectButton = new JButton("Connect");

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;
            add(comboBox, gbc);

            gbc.gridx++;
            gbc.weightx = 1;
            add(connectButton, gbc);
        }

    }

    public class LoginPane extends JPanel {

        private JTextField userNameField;
        private JPasswordField passwordField;
        private JButton loginButton;

        public LoginPane() {

            userNameField = new JTextField(10);
            passwordField = new JPasswordField(10);
            loginButton = new JButton("Login");

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;
            add(new JLabel("User name:"), gbc);

            gbc.gridx++;
            add(userNameField, gbc);

            gbc.gridx++;
            add(new JLabel("Password:"), gbc);

            gbc.gridx++;
            add(passwordField, gbc);

            gbc.gridx++;
            gbc.weightx = 1;
            add(loginButton, gbc);

        }

    }

}
Run Code Online (Sandbox Code Playgroud)