java之swing单选框用法实例分析

时间:2022-03-24 21:11:57

本文实例讲述了java之swing单选框用法。分享给大家供大家参考。具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class test extends JApplet
implements ActionListener{
  JTextField jtf;
  public void init(){
    Container contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    JRadioButton b1 = new JRadioButton("A");
    b1.addActionListener(this);
    contentPane.add(b1);
    JRadioButton b2 = new JRadioButton("B");
    b2.addActionListener(this);
    contentPane.add(b2);
    JRadioButton b3 = new JRadioButton("C");
    b3.addActionListener(this);
    contentPane.add(b3);
    ButtonGroup bg = new ButtonGroup();
    bg.add(b1);
    bg.add(b2);
    bg.add(b3);
    jtf = new JTextField(15);
    contentPane.add(jtf);
  }
  public void actionPerformed(ActionEvent ae){
    jtf.setText(ae.getActionCommand());
  }
}

希望本文所述对大家的java程序设计有所帮助。