当前位置: 首页 > 知识库问答 >
问题:

使组件免疫GridBagConstrains.fillGridBagLayout

宗波涛
2023-03-14

我有一个小Java摆动图形用户界面与GridBagLayout。我已经设置了GridBagConstraints.fill=GridBagConstraint。两者,但不希望我的按钮或文本字段得到垂直调整大小。此外,我只希望我的JTextArea调整大小,并尝试为我不想调整大小的组件设置最大大小,但它不起作用。关于如何使GBC。填充只适用于某些组件的任何想法?

可复制的例子:

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

共有1个答案

郎聪
2023-03-14

关于如何制作GBC的任何想法。填充仅适用于某些组件?

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

正如Abra提到的,是的,您可以通过根据需要更改一个或多个字段的设置来修改GridBagConstraint(GBC)对象,我有时也这样做,但更常见的是,我使用助手方法来动态创建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);
        });
    }
}
 类似资料:
  • 我正在制作井字游戏应用程序,第一次点击按钮后,它会得到“O”或“X”文本,我想避免第二次点击,就像我想让它免受第二次点击一样 到目前为止,我得到了这个,

  • 本文向大家介绍Java接口中尽量避免使用数组,包括了Java接口中尽量避免使用数组的使用技巧和注意事项,需要的朋友参考一下  如果你发现在一个接口使用有如下定义方法:   那么你应该认真反思。数组不仅仅老式,而且我们有合理的理由避免暴露它们。在这篇文章中,我将试图总结在Java API中使用数组的缺陷。首先从最出人意料的一个例子开始。 数组导致性能不佳 你可能认为使用数组是最快速的,因为数组是大多

  • 我所有的主要组件都有如下部分: 有了这个,我正在检查是否有用户登录。如果这是假的,则用户将被重定向到登录路由。由于这个部分用于许多组件,我在想如果我可以优化这个得到一个DRY代码... 使现代化 我正在使用反应路由器:

  • 全局注册 我们已经知道,可以通过以下方式创建一个 Vue 实例: new Vue({ el: '#some-element', // 选项 }) 要注册一个全局组件,可以使用Vue.component(tagName, options)。例如: Vue.component('my-component', { // 选项 }) 请注意,对于自定义标签的命名 Vue.js 不强制遵

  • 让我们看一下使用两种不同的方式去计算单词的个数,第一种方式使用 reduceByKey 另外一种方式使用 groupByKey: val words = Array("one", "two", "two", "three", "three", "three") val wordPairsRDD = sc.parallelize(words).map(word => (word, 1)) val