提问者:小点点

使用GridBagLayout调整大小时防止组件散开


编辑:每次我尝试添加< code>gui标记时,它都会切换到< code >用户界面。有人介意解释一下吗?

我希望客户端可以调整大小。我希望< code>JSeparator在调整大小时填充框架的宽度,但是我希望< code>JLabel保持在字段旁边。

它是这样开始的,JLabels与田野相距太远:

当我水平调整它的大小时,结果是:

这显然相距太远了。我用来设置这些组件的代码是:

public class LoginPanel extends JPanel {
    
    private JTextField userfield = new JTextField(10);
    private JPasswordField passfield = new JPasswordField(10);
    private JButton login = new JButton("Login");
    private JButton create = new JButton("Create Account");
    
    public LoginPanel() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.weightx = 1;
        gbc.gridx = 2;
        JLabel label = new JLabel("Username: ");
        add(label, gbc);
        
        gbc.gridx = 3;
        gbc.gridwidth = 2;
        add(userfield, gbc);
        
        gbc.gridy = 1;
        add(passfield, gbc);
        
        gbc.gridx = 2;
        label = new JLabel("Password: ");
        add(label, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridy = 2;
        gbc.gridx = 1;
        gbc.gridwidth = 5;
        add(new JSeparator(JSeparator.HORIZONTAL), gbc);
    }
}

(不得不删掉一些东西,请告诉我我是否遗漏了什么)我已经尝试过锚定,但我仍然不是100%熟悉GridBagLayout(和约束),所以我不确定我的尝试是否朝着正确的方向前进。

如何防止用户名:密码:标签离开我的字段,同时仍然能够调整大小?

此外,我想使用GridBagLayout。我还有很多东西需要添加,我不想使用简单的布局,因为我需要灵活性。


共1个答案

匿名用户

使用GridBagConstraints#anchor

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LogInTest {

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

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

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

    public class LoginPanel extends JPanel {

        private JTextField userfield = new JTextField(10);
        private JPasswordField passfield = new JPasswordField(10);
        private JButton login = new JButton("Login");
        private JButton create = new JButton("Create Account");

        public LoginPanel() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();

            gbc.anchor = GridBagConstraints.CENTER;
            gbc.weightx = 1;
            gbc.gridx = 2;
            gbc.anchor = GridBagConstraints.EAST;
            JLabel label = new JLabel("Username: ");
            add(label, gbc);

            gbc.anchor = GridBagConstraints.WEST;
            gbc.gridx = 3;
            gbc.gridwidth = 2;
            add(userfield, gbc);

            gbc.gridy = 1;
            add(passfield, gbc);

            gbc.anchor = GridBagConstraints.EAST;
            gbc.gridx = 2;
            label = new JLabel("Password: ");
            add(label, gbc);

            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridy = 2;
            gbc.gridx = 1;
            gbc.gridwidth = 5;
            add(new JSeparator(JSeparator.HORIZONTAL), gbc);
        }
    }

}

您可能还需要考虑使用复合布局,即将每个区域分离到其自己的容器中,并专注于每个部分的单独布局需求,然后将它们全部构建到单个布局中。