java - GridBagConstraints 未正确对齐 JRadioButton 框

标签 java swing user-interface layout jradiobutton

即使指定了 .weightx、.weighty 和 .anchor,GridBagConstraint 也不起作用。 我需要将组件设置在左上角,但它一直设置在屏幕中央。以下是我定位组件的方法。

private void initGUI(){
    getContentPane().setLayout(new GridBagLayout());

    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(450, 450));

    Box buttonBox = Box.createVerticalBox();
    ButtonGroup buttons = new ButtonGroup();

    buttons.add(okOnly);
    buttons.add(okCancel);
    buttons.add(yesNoCancel);
    buttons.add(yesNo);

    buttonBox.add(okOnly);
    buttonBox.add(okCancel);
    buttonBox.add(yesNoCancel);
    buttonBox.add(yesNo);
    buttonBox.setBorder(BorderFactory.createTitledBorder("Buttons"));

    GridBagConstraints gridConstraints = new GridBagConstraints();
    gridConstraints.gridx = 0;
    gridConstraints.gridy = 0;
    gridConstraints.anchor = GridBagConstraints.NORTHWEST;
    gridConstraints.weightx = 1;
    gridConstraints.weighty = 1;

    panel.add(buttonBox, gridConstraints);
    getContentPane().add(panel);
}

最佳答案

您将 GridBagLayout 赋予 contentPane,但在没有 GridBagConstraints 的情况下向其添加组件,然后为面板 JPanel 赋予布局,以便它使用默认的 FlowLayout,并添加组件使用 GridBagConstraints 对其进行修改,这些约束在这种情况下没有任何意义。

解决方案:显然不要这样做。仅在将组件添加到使用 GridBagLayout 的容器时才使用 GridBagConstraints。

事实上,如果您只需要左上角的 JRadioButtons 集合,我会简单地删除面板 JPanel:

import java.awt.*;
import javax.swing.*;

public class Gui extends JFrame {
    private JRadioButton okOnly = new JRadioButton("Ok ");
    private JRadioButton okCancel = new JRadioButton("Ok Cancel");
    private JRadioButton yesNoCancel = new JRadioButton("Yes No Cancel");
    private JRadioButton yesNo = new JRadioButton("Yes No");

    private void initGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().setLayout(new GridBagLayout());

        // JPanel panel = new JPanel();
        // panel.setPreferredSize(new Dimension(450, 450));

        setPreferredSize(new Dimension(450, 450));

        Box buttonBox = Box.createVerticalBox();
        ButtonGroup buttons = new ButtonGroup();

        buttons.add(okOnly);
        buttons.add(okCancel);
        buttons.add(yesNoCancel);
        buttons.add(yesNo);

        buttonBox.add(okOnly);
        buttonBox.add(okCancel);
        buttonBox.add(yesNoCancel);
        buttonBox.add(yesNo);
        buttonBox.setBorder(BorderFactory.createTitledBorder("Buttons"));

        GridBagConstraints gridConstraints = new GridBagConstraints();
        gridConstraints.gridx = 0;
        gridConstraints.gridy = 0;
        gridConstraints.anchor = GridBagConstraints.NORTHWEST;
        gridConstraints.weightx = 1;
        gridConstraints.weighty = 1;

        // panel.add(buttonBox, gridConstraints);
        add(buttonBox, gridConstraints);
        // getContentPane().add(panel);

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private static void createAndShowGui() {
        new Gui().initGUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

关于java - GridBagConstraints 未正确对齐 JRadioButton 框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39840703/

相关文章:

java - 如何调试 ContextLoader - 上下文初始化失败和 BeanCreationException

java - 无法从我的 main 调用 JFrame 方法

ios - 显示应用程序首次打开的日期

java - 如何判断我的 JComponent 是否正在接收来自软件的重绘调用?

python - gtk 像在 gtk.SpinButton 中一样创建小的向上 + 向下按钮

oracle - 创建具有多个页面的 Apex 表单

java - 如何在 Java 中跨多个类使用同一个 Scanner

java - 单击按钮时无法打开新 Activity

java - 对象被标记为 "javax.validation.constraints.NotNull"但未在此构造函数中初始化

java - 防止关闭 xchart 图表时关闭整个 JVM