本文实例讲述了java之swing实现复选框的方法。分享给大家供大家参考。具体如下:
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());
}
}