简体   繁体   English

多个面板相互下方

[英]Multiple Panels Below Each Other

Is it possible i can have a top panel for a login.我可以有一个用于登录的顶部面板吗? Then a middle and bottom panel underneath.然后是下面的中间和底部面板。 I'm using gridbaglayout and i have assigned 2 panels.我正在使用 gridbaglayout 并分配了 2 个面板。 One panel has components linked to it for login details and the other panel has other components linked to it.一个面板链接了用于登录详细信息的组件,另一个面板链接了其他组件。 However, it's not going occurring to plan.但是,计划不会发生。 Ive tried using Box/Borderlayout to do this, but then i'm not able to create the spacing i want.我试过使用 Box/Borderlayout 来做到这一点,但后来我无法创建我想要的间距。

Here is a sketch of what i want.这是我想要的草图。

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

I'd break down the application into it's individual areas of responsibilities and focus on creating the UI elements to support it.我会将应用程序分解为它的各个职责领域,并专注于创建 UI 元素来支持它。 This will allow you to focus on the needs of each section of your application, it also allows you the opportunity to change around the layout as you need这将使您能够专注于应用程序每个部分的需求,它还使您有机会根据需要更改布局

在此处输入图片说明

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

        }

    }

}

To start with, replace the call to GUI.setSize(750,500) with GUI.pack() .首先,用GUI.pack()替换对GUI.setSize(750,500)的调用。 That will size the window to fit your content.这将调整窗口大小以适合您的内容。 After doing that you'll see the second panel is appearing to the right of the first.这样做之后,您会看到第二个面板出现在第一个面板的右侧。 This is because you did not set gridx / gridy on GBC2 so it defaults to putting myTopPL2 to the right of myTopPL1 , not below it.这是因为你没有设置gridx / gridyGBC2所以它默认为把myTopPL2到右侧myTopPL1 ,不能低于它。

I rarely explicitly set the gridx / gridy when using GridBagLayout .使用GridBagLayout时,我很少明确设置gridx / gridy For your first row of the display, I would use default values for all but myLoginBN where I would set gridwidth to GridBagConstraints.REMAINDER .对于显示的第一行,除了myLoginBN ,我将使用默认值,其中我将gridwidth设置为GridBagConstraints.REMAINDER That tells GridBagLayout that nothing else goes on this row.这告诉 GridBagLayout 该行没有其他内容。 The next item added with a default gbc will be placed at the start of the second row.添加了默认 gbc 的下一个项目将放置在第二行的开头。

Minor nits:小尼特:

  • Use standard Java capitalization:使用标准的 Java 大小写:
    • Class name are camelcase with a leading uppercase character.类名是驼峰式的,前导大写字符。 So, "Gui", not "GUI".所以,“GUI”,而不是“GUI”。
    • Instance names are camelcase with a leading lowercase character.实例名称以驼峰命名,带前导小写字符。 So, "gbc1" not "GBC1"所以,“gbc1”不是“GBC1”
  • Use SwingUtilities.InvokeLater to start the Gui on the EDT使用 SwingUtilities.InvokeLater 在 EDT 上启动 Gui

     SwingUtilities.invokeLater(new Runnable() { public void run() { GUI GUI = new GUI (); GUI .setDefaultCloseOperation(RMIAuctionHouse.EXIT_ON_CLOSE); GUI .setSize(750,500); GUI .setVisible(true); }});

Seems you are not familiar with Java layouts, so i recomend you to try null(absolute) Layout:似乎您不熟悉 Java 布局,因此我建议您尝试使用 null(absolute) 布局:

setLayout(null);
...
component.setBounds( x, y, width, heigh );
add(component);

Here example这里的例子

package gui;

import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class GUI extends JFrame{

    private JTextField myUsernameTF;
    private JLabel myUsernameLBL;

    public GUI() {

        super("GUI ");
        setLayout(null);     

        myUsernameLBL = new JLabel("Username: ");
        myUsernameLBL.setFont(new Font("Arial", Font.BOLD, 13));
        myUsernameLBL.setBounds(50,30,80,20);
        add(myUsernameLBL);

        myUsernameTF = new JTextField(10);
        myUsernameTF.setBounds(190,30,80,20);
        add(myUsernameTF);

        // and so on ...



    }

    public static void main(String[] args) {
        GUI frame = new GUI();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(750,500);
        frame.setVisible(true);
    }
}

Good luck on thorny way to handle java swing =)祝你在处理 java swing 的棘手方式上好运 =)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM