java之swing实现复选框的方法

时间:2022-09-03 11:45:31

本文实例讲述了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
30
31
32
33
34
35
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class test extends JApplet
implements ItemListener{
  JTextField jtf;
  public void init(){
    Container contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    ImageIcon a1 = new ImageIcon("D:/data/Images/6.gif");
    ImageIcon a2 = new ImageIcon("D:/data/Images/7.gif");
    ImageIcon a3 = new ImageIcon("D:/data/Images/8.gif");
    JCheckBox cb = new JCheckBox("C", a1);
    cb.setRolloverIcon(a2);
    cb.setSelectedIcon(a3);
    cb.addItemListener(this);
    contentPane.add(cb);
    cb = new JCheckBox("C++", a1);
    cb.setRolloverIcon(a2);
    cb.setSelectedIcon(a3);
    cb.addItemListener(this);
    contentPane.add(cb);
    cb = new JCheckBox("java", a1);
    cb.setRolloverIcon(a2);
    cb.setSelectedIcon(a3);
    cb.addItemListener(this);
    contentPane.add(cb);
    jtf = new JTextField(15);
    contentPane.add(jtf);
  }
  public void itemStateChanged(ItemEvent ie){
    JCheckBox cb = (JCheckBox)ie.getItem();
    jtf.setText(cb.getText());
  }
}

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