首页 > 其他 > 详细

JRadioButton

时间:2014-08-26 21:25:56      阅读:439      评论:0      收藏:0      [点我收藏+]
 1 public class SwingTest123 extends JFrame implements ActionListener {
 2 
 3     JRadioButton boy, girl;
 4     JLabel mess;
 5     ButtonGroup group;
 6 
 7     public SwingTest123() {
 8         init();
 9         setBounds(100, 200, 200, 200);
10         setVisible(true);
11     }
12 
13     public void init() {
14         setLayout(null);
15         Container c = getContentPane();
16      
17         boy = new JRadioButton("boy");
18         boy.setBounds(20, 50, 50, 35);
19         girl = new JRadioButton("girl");
20         girl.setBounds(100, 50, 50, 35);
21         mess = new JLabel("hello");
22         mess.setBounds(30, 100, 100, 25);
23 
24         group = new ButtonGroup();
25         group.add(boy);
26         group.add(girl);
27         c.add(boy);
28         c.add(girl);
29         c.add(mess);
30         boy.addActionListener(this);
31         girl.addActionListener(this);
32 
33     }
34 
35     String radioText = null;
36 
37     @Override
38     public void actionPerformed(ActionEvent e) {
39         if (e.getSource() == boy) {
40             radioText = boy.getText();
41 
42             System.out.println(radioText);
43         } else if (e.getSource() == girl) {
44             radioText = girl.getText();
45             System.out.println(radioText);
46         }
47         mess.setText("You are a " + radioText);
48 
49     }
50 
51     public static void main(String[] args) {
52         SwingTest123 st = new SwingTest123();
53 
54     }
55 
56 }

 

bubuko.com,布布扣

 

JRadioButton

原文:http://www.cnblogs.com/thrive/p/3938119.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!