我有一个带有标签和两个单选按钮的 jframe。
我使用弹簧布局,但我的第二个 radioButton 在页面左上角看到!
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
我想在单选按钮行的下方显示我的两个按钮!