Swing中的单选按钮,我用的是JRadioButton,但不知道为什么还是多选的。可以帮我看看不?~!谢啦~!
importjava.awt.Container;importjava.awt.GridLayout;importjava.awt.event.WindowAdapter...
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
publicclass JRadioButtonTest {
JFrame f = null ;
JRadioButton r4 = null ;
JRadioButton r5 = null ;
JRadioButtonTest(){
f = new JFrame("single checkbox example");
Container contentPane = f. getContentPane();
contentPane.setLayout(new GridLayout(2,1));
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1,3));
p1.setBorder(BorderFactory.createTitledBorder("choose your favourite city"));
JRadioButton r1 = new JRadioButton ("beijing");
JRadioButton r2 = new JRadioButton ("shanghai");
JRadioButton r3 = new JRadioButton ("qingdao");
p1.add(r1);
p1.add(r2);
p1.add(r3);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(2,1));
contentPane.add(p1);
contentPane.add(p2);
f.pack();
f.show();
f.addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
});
}
publicstaticvoid main (String [] args ){
new JRadioButtonTest();
}
} 展开
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
publicclass JRadioButtonTest {
JFrame f = null ;
JRadioButton r4 = null ;
JRadioButton r5 = null ;
JRadioButtonTest(){
f = new JFrame("single checkbox example");
Container contentPane = f. getContentPane();
contentPane.setLayout(new GridLayout(2,1));
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1,3));
p1.setBorder(BorderFactory.createTitledBorder("choose your favourite city"));
JRadioButton r1 = new JRadioButton ("beijing");
JRadioButton r2 = new JRadioButton ("shanghai");
JRadioButton r3 = new JRadioButton ("qingdao");
p1.add(r1);
p1.add(r2);
p1.add(r3);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(2,1));
contentPane.add(p1);
contentPane.add(p2);
f.pack();
f.show();
f.addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
});
}
publicstaticvoid main (String [] args ){
new JRadioButtonTest();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询