JRadioButton

 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 }

 

 

转载于:https://www.cnblogs.com/thrive/p/3938119.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值