简体   繁体   English

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

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

I have a little Java swing GUI with a GridBagLayout.我有一个带有 GridBagLayout 的小 Java swing GUI。 I have set the GridBagConstraints.fill = GridBagConstraints.BOTH but don't want that my buttons or text fields to get resized vertically.我已经设置了GridBagConstraints.fill = GridBagConstraints.BOTH但不希望我的按钮或文本字段垂直调整大小。 Furthermore, I only want my JTextArea to be resized and tried putting in maximum size for the components I don't want to size up, but it is not working.此外,我只希望调整JTextArea的大小并尝试为我不想调整大小的组件设置最大大小,但它不起作用。 Any ideas on how I can make the GBC.fill only applicable for some components?关于如何使 GBC.fill 仅适用于某些组件的任何想法?

Reproducible example:可重现的例子:

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

Any ideas on how I can make the GBC.fill only applicable for some components?关于如何使 GBC.fill 仅适用于某些组件的任何想法?

Yes.是的。 Don't have all components use a GridBagConstraints object whose fill field is .BOTH .不要让所有组件都使用填充字段为.BOTH Each component should be added with its own GBC object, and the ones you want filled both horizontally and vertically should have the fill property set to .BOTH , and the components that should only expand horizontally should have GBC fill field of .HORIZONTAL .每个组件都应添加其自己的 GBC object,并且要水平和垂直填充的组件应将填充属性设置为.BOTH ,并且仅应水平扩展的组件应具有.HORIZONTAL的 GBC 填充字段。 When I use the GridBagLayout, I often create helper method(s) for creating constraint objects, methods that know what settings to use based on parameters passed into them, and I suggest that you consider doing the same.当我使用 GridBagLayout 时,我经常创建帮助方法来创建约束对象,这些方法根据传递给它们的参数知道要使用哪些设置,我建议您考虑这样做。


As Abra mentions, yes, you can modify a GridBagConstraints (GBC) object, by changing the settings of one or more fields as necessary, and I sometimes do this too, but more often, I use helper methods to create GBC objects on the fly.正如 Abra 提到的,是的,您可以通过根据需要更改一个或多个字段的设置来修改 GridBagConstraints (GBC) object,我有时也会这样做,但更常见的是,我使用辅助方法动态创建 GBC 对象. It just works better for me that way.这样对我来说效果更好。


For example:例如:

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