首页 > 代码库 > JRadioButton
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 @Override38 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 }
JRadioButton
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。