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

时间:2014-05-20 04:59:00

标签: java swing user-interface layout gridbaglayout

修改:每次我尝试添加gui标记时,都会切换到user-interface。有人在解释/解决这个问题吗?

我希望客户端可以调整大小。我希望JSeparator在调整大小时填充框架的宽度,但我希望JLabel s留在字段旁边。

它的开头是这样的,JLabel与现场相距太远:

Small

当我水平调整大小时,结果如下:

Resized

这显然太过分了。我用来设置这些组件的代码是:

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(和约束),所以我不确定我的尝试是否正确。

如何阻止Username:Password:标签离开我的字段,仍然能够调整大小?

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

1 个答案:

答案 0 :(得分:3)

利用GridBagConstraints#anchor

login login

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

}

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