當我運行下面的代碼時,前2個按鈕不會出現;然而,第三個在框架中是可見的。程序沒有返回適當的RadioButton結果
public class RadioButton {
RadioButton(){
JFrame frame = new JFrame();
JRadioButton button1 = new JRadioButton("One");
button1.setBounds(50, 20, 50, 20);
JRadioButton button2 = new JRadioButton("Two");
button2.setBounds(50, 50, 50, 20);
JRadioButton button3 = new JRadioButton("Three");
button2.setBounds(50, 80, 50, 20);
ButtonGroup bg = new ButtonGroup();
bg.add(button1);
bg.add(button2);
bg.add(button3);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new RadioButton();
}
}
我注意到你在button2上設置了兩次邊界,而從不在button3上。錯字? –