[英]Inconsistent placement of components (button and checkbox) using GridBagLayout
The code below was copied from Oracle tutorials and modified to place components OTHER than buttons--a label and two text fields and an additional button. 下面的代码是从Oracle教程中复制并修改为放置除按钮以外的组件 - 标签和两个文本字段以及一个附加按钮。 It produces the form that it should (shown below), so you shouldn't have to scrutinize the first 30 or so lines of code:
它产生它应该的形式(如下所示),所以你不必仔细检查前30行左右的代码:
If I UN comment out the two lines that refer to "box" and comment out the adjoining lines that refer to button
, look at where the checkbox is placed. 如果我UN注释掉引用“box”的两行并注释掉引用
button
的相邻行,请查看复选框的放置位置。
I can't figure out how what's going on. 我无法弄清楚是怎么回事。 I want the checkbox on the last line , as the code SEEMS to say it MUST be positioned, yet it is NOT.
我想要最后一行的复选框 ,因为代码SEEMS要说它必须定位,但它不是。
All I am doing is changing TWO LINES that refer to button to refer instead to checkbox. 我正在做的就是更改两个引用按钮的行,而不是复选框。 Are the rules for GridBagLayout component-dependent??
GridBagLayout的规则是否依赖于组件? What should I do (and why isn't the code working as is) to get checkbox on bottom line?
我应该怎么做(为什么代码不能正常工作)才能获得底线复选框?
public class Testy {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0; c.gridy = 0;
JLabel label = new JLabel("Button 1 ");
pane.add(label, c);
JTextField text = new JTextField("Button 2 ");
c.gridx = 1; c.gridy = 0;
pane.add(text, c);
JButton button = new JButton("Button 3 ");
c.gridx = 2; c.gridy = 0;
pane.add(button, c);
text = new JTextField("LONG Text 4 ");
c.ipady = 40;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0; c.gridy = 1;
pane.add(text, c);
button = new JButton("5 ");
c.ipady = 0;
c.weighty = 1.0;
c.gridx = 1; c.gridy = 2;
c.gridwidth = 2;
pane.add(button, c);
// Here's the part of the code in question
button = new JButton("666"); // This placement is FINE, at bottom of form.
// JCheckBox box = new JCheckBox("Me?"); // "box" does NOT get placed on last line.
c.weighty = 0;
c.gridx = 0; c.gridy = 3;
c.gridwidth = 1;
// pane.add(box);
pane.add(button, c);
}
Here are the required imports and two methods that I removed so the problem code area above could be seen without scrolling: 以下是我删除的必需导入和两个方法,因此可以在不滚动的情况下看到上面的问题代码区域:
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
private static void createAndShowGUI() /* calls addComponentsToPane() */{
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) /* calls createAndShowGUI() */ {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
} }); }
}
// pane.add(box);
pane.add(button, c);
You just have to use the right method: 你只需要使用正确的方法:
pane.add(box, c);
See Container#add(Component comp) and Container#add(Component comp, Object constraints) . 请参阅Container#add(组件comp)和Container#add(组件comp,对象约束) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.