GridBagConstraints 未正确对齐 JRadioButton 框
GridBagConstraints Not Correctly Aligning JRadioButton Box
即使指定了 .weightx、.weighty 和 .anchor,GridBagConstraints 也不起作用。
我需要将组件设置在左上角,但它一直设置在屏幕中央。下面是我用于定位组件的方法。
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 的组件,然后将 no 布局提供给面板 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());
}
}
即使指定了 .weightx、.weighty 和 .anchor,GridBagConstraints 也不起作用。 我需要将组件设置在左上角,但它一直设置在屏幕中央。下面是我用于定位组件的方法。
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 的组件,然后将 no 布局提供给面板 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());
}
}