繁体   English   中英

在 GridBagLayout 中使组件不受 GridBagConstrains.fill 的影响

[英]Make components immune to GridBagConstrains.fill in GridBagLayout

我有一个带有 GridBagLayout 的小 Java swing GUI。 我已经设置了GridBagConstraints.fill = GridBagConstraints.BOTH但不希望我的按钮或文本字段垂直调整大小。 此外,我只希望调整JTextArea的大小并尝试为我不想调整大小的组件设置最大大小,但它不起作用。 关于如何使 GBC.fill 仅适用于某些组件的任何想法?

可重现的例子:

public class Main {
    static JFrame frame = new JFrame("Create New Entry");
    static JPanel wrapper = new JPanel();
    static JTextField title = new JTextField(20);
    static JLabel titleLabel = new JLabel("Title");
    static JTextField username = new JTextField(20);
    static JLabel usernameLabel = new JLabel("Username");
    static JTextField email = new JTextField(20);
    static JLabel emailLabel = new JLabel("Email Address");
    static JPasswordField password = new JPasswordField(20);
    static JLabel passwordLabel = new JLabel("Password");
    static JButton generatePassword = new JButton("Generate Password");
    static JPasswordField confirmPassword = new JPasswordField();
    static JLabel confirmPasswordLabel = new JLabel("Confirm Password");
    static JToggleButton showPassword = new JToggleButton("Show Password");
    static JButton submit = new JButton("Submit Entry");
    static JLabel notesLabel = new JLabel("Notes");
    static JTextArea textArea = new JTextArea(5, 0);
    static JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    public static void main(String[] args) {

        GridBagConstraints gbc = new GridBagConstraints();
        wrapper.setLayout(new GridBagLayout());
        gbc.gridwidth = GridBagConstraints.RELATIVE;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.weightx = 1;
        gbc.weighty = 1;

        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 0;
        wrapper.add(titleLabel, gbc);
        gbc.gridx = 1;
        gbc.gridwidth = 2;
        wrapper.add(title, gbc);

        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 1;
        wrapper.add(usernameLabel, gbc);
        gbc.gridx = 1;
        gbc.gridwidth = 2;
        wrapper.add(username, gbc);

        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 2;
        wrapper.add(emailLabel, gbc);
        gbc.gridx = 1;
        gbc.gridwidth = 2;
        wrapper.add(email, gbc);

        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 3;
        wrapper.add(passwordLabel, gbc);
        gbc.gridx = 1;
        wrapper.add(password, gbc);
        gbc.gridx = 2;
        wrapper.add(generatePassword, gbc);

        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 4;
        wrapper.add(confirmPasswordLabel, gbc);
        gbc.gridx = 1;
        wrapper.add(confirmPassword, gbc);
        gbc.gridx = 2;
        wrapper.add(showPassword, gbc);

        gbc.gridx = 0;
        gbc.gridy = 5;
        wrapper.add(notesLabel, gbc);
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 1;
        gbc.gridwidth = 2;
        wrapper.add(scrollPane, gbc);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 3;
        gbc.gridy = 6;
        wrapper.add(submit, gbc);
        
        password.setPreferredSize(new Dimension(300, 26));
        confirmPassword.setPreferredSize(new Dimension(300, 26));
        title.setPreferredSize(new Dimension(300, 26));
        username.setPreferredSize(new Dimension(300, 26));
        email.setPreferredSize(new Dimension(300, 26));

        frame.add(wrapper);
        frame.pack();
        frame.setMinimumSize(frame.getSize());
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

关于如何使 GBC.fill 仅适用于某些组件的任何想法?

是的。 不要让所有组件都使用填充字段为.BOTH 每个组件都应添加其自己的 GBC object,并且要水平和垂直填充的组件应将填充属性设置为.BOTH ,并且仅应水平扩展的组件应具有.HORIZONTAL的 GBC 填充字段。 当我使用 GridBagLayout 时,我经常创建帮助方法来创建约束对象,这些方法根据传递给它们的参数知道要使用哪些设置,我建议您考虑这样做。


正如 Abra 提到的,是的,您可以通过根据需要更改一个或多个字段的设置来修改 GridBagConstraints (GBC) object,我有时也会这样做,但更常见的是,我使用辅助方法动态创建 GBC 对象. 这样对我来说效果更好。


例如:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;

public class Main2 extends JPanel {
    private static final long serialVersionUID = 1L;
    private static final int FIELD_COLS = 20;
    private static final int TA_ROWS = 10;
    private static final int INGS_GAP = 10;
    private JLabel titleLabel = new JLabel("Title");
    private JLabel userNameLabel = new JLabel("Username");
    private JLabel emailAddrLabel = new JLabel("EmailAddress");
    private JLabel passwordLabel = new JLabel("Password");
    private JLabel confirmPasswordLabel = new JLabel("Confirm Password");
    private JLabel notesLabel = new JLabel("Notes");

    private JTextField titleField = new JTextField(FIELD_COLS);
    private JTextField userNameField = new JTextField(FIELD_COLS);
    private JTextField emailField = new JTextField(FIELD_COLS);
    private JPasswordField passwordField = new JPasswordField(FIELD_COLS);
    private JPasswordField confirmPasswordField = new JPasswordField(FIELD_COLS);

    private JTextArea NotesArea = new JTextArea(TA_ROWS, FIELD_COLS);
    private JScrollPane textAreaScrollPane = new JScrollPane(NotesArea);

    private JButton generatePasswordBtn = new JButton("Generate Password");
    private JButton showPasswordBtn = new JButton("Show Password");
    private JButton submitEntryBtn = new JButton("Submit Entry");

    public Main2() {
        setLayout(new GridBagLayout());

        // basic GBC use
        int row = 0;
        GridBagConstraints gbc = createGbc(0, row, GridBagConstraints.HORIZONTAL);
        add(titleLabel, gbc);
        gbc = createGbc(1, row, GridBagConstraints.HORIZONTAL, 2, 1);
        add(titleField, gbc);

        row++;
        gbc = createGbc(0, row, GridBagConstraints.HORIZONTAL);
        add(userNameLabel, gbc);
        gbc = createGbc(1, row, GridBagConstraints.HORIZONTAL, 2, 1);
        add(userNameField, gbc);

        row++;
        gbc = createGbc(0, row, GridBagConstraints.HORIZONTAL);
        add(emailAddrLabel, gbc);
        gbc = createGbc(1, row, GridBagConstraints.HORIZONTAL, 2, 1);
        add(emailField, gbc);

        row++;
        gbc = createGbc(0, row, GridBagConstraints.HORIZONTAL);
        add(passwordLabel, gbc);
        gbc = createGbc(1, row, GridBagConstraints.HORIZONTAL);
        add(passwordField, gbc);
        gbc = createGbc(2, row, GridBagConstraints.HORIZONTAL);
        add(generatePasswordBtn, gbc);

        row++;
        gbc = createGbc(0, row, GridBagConstraints.HORIZONTAL);
        add(confirmPasswordLabel, gbc);
        gbc = createGbc(1, row, GridBagConstraints.HORIZONTAL);
        add(confirmPasswordField, gbc);
        gbc = createGbc(2, row, GridBagConstraints.HORIZONTAL);
        add(showPasswordBtn, gbc);

        // here we set the GBC weighty to non-0 value to allow vertical expansion
        // of added components
        row++;
        int txtAreaRows = TA_ROWS;
        gbc = createGbc(0, row, GridBagConstraints.HORIZONTAL);
        gbc.anchor = GridBagConstraints.WEST;
        gbc.weighty = 1.0;
        add(notesLabel, gbc);
        gbc = createGbc(1, row, GridBagConstraints.BOTH, 2, txtAreaRows);
        gbc.weighty = 1.0;
        add(textAreaScrollPane, gbc);
        textAreaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        row += txtAreaRows;
        gbc = createGbc(0, row, GridBagConstraints.HORIZONTAL);
        add(new JLabel(""), gbc);
        gbc = createGbc(1, row, GridBagConstraints.HORIZONTAL, 2, 1);
        add(submitEntryBtn, gbc);
    }

    private static GridBagConstraints createGbc(int x, int y, int fill, int width, int height) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = height;
        gbc.fill = fill;

        // allow horizontal expansion *unless* at left-most position in row
        gbc.weightx = x == 0 ? 0.0 : 1.0;
        gbc.weighty = 0.0; // default to no vertical expansion

        gbc.insets = new Insets(INGS_GAP, INGS_GAP, INGS_GAP, INGS_GAP);
        return gbc;
    }

    private static GridBagConstraints createGbc(int x, int y, int fill) {
        return createGbc(x, y, fill, 1, 1);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            Main2 mainPanel = new Main2();

            JFrame frame = new JFrame("GUI");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(mainPanel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

暂无
暂无

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

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