我有一个 jframe,它有一个标签和两个单选按钮。
我使用 spring 布局,但我的第二个单选按钮出现在页面左上角!
public class tester extends JFrame {
public tester() {
add(create());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 500);
setVisible(true);
}
public JPanel create() {
JPanel panel = new JPanel();
ButtonGroup group = new ButtonGroup();
JRadioButton r1 = new JRadioButton("Yes");
JRadioButton r2 = new JRadioButton("No");
group.add(r1);
group.add(r2);
JLabel lable = new JLabel("Today is sunday?");
panel.add(lable);
// panel.add(group); // How add this?
panel.add(r1);
panel.add(r2);
JButton savebt= new JButton("Save");
JButton cancelbt=new JButton("Cancell");
panel.add(savebt);
panel.add(cancelbt);
panel.setLayout(new SpringLayout());
SpringUtilities.makeCompactGrid(panel, 1, 3, 50, 100, 25, 50);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new tester();
}
});
}
}
现在出现这个异常:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: No such child: 5
我想在单选按钮行的下方显示我的两个按钮!
最佳答案
面板中有 3 个项目,因此列数应为 3:
SpringUtilities.makeCompactGrid(panel, 1, 3, 50, 100, 25, 50);
// panel.add(group); // How add this?
你不需要这个。 ButtonGroups 不会添加到面板中。它们用于按钮选择管理,不显示。
关于java - 使用 Spring 布局用于 Hand GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15669680/