import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* Title:演示JCheckBox使用
* Description:通过勾选不同的复选框来显示不同的图片
* @author fan
*
*/
public class MyCheckDemo extends JFrame implements ItemListener{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 定义四个复选框
*/
JCheckBox chinCheck, glassesCheck, hairCheck, teetchCheck;
/**
* 定义一个JLabel用来进行图片的显示
*/
JLabel pictureLabel;
/**
* 定义一个StringBuffer来标记需要进行显示的图片,利用setChar(int, char)来进行
*/
StringBuffer choices = new StringBuffer("cght");
/**
* 构造方法
*/
public MyCheckDemo(){
lunchFrame();
}
/**
* 进行界面的构造
*/
public void lunchFrame(){
this.setLayout(new BorderLayout());
JPanel check_Panel = new JPanel();
chinCheck = new JCheckBox("下巴(c)");
glassesCheck = new JCheckBox("眼镜(g)");
hairCheck = new JCheckBox("头发(h)");
teetchCheck = new JCheckBox("牙齿(t)");
chinCheck.setSelected(true);
glassesCheck.setSelected(true);
hairCheck.setSelected(true);
teetchCheck.setSelected(true);
chinCheck.addItemListener(this);
glassesCheck.addItemListener(this);
hairCheck.addItemListener(this);
teetchCheck.addItemListener(this);
check_Panel.setLayout(new GridLayout(0, 1));
check_Panel.add(chinCheck);
check_Panel.add(glassesCheck);
check_Panel.add(hairCheck);
check_Panel.add(teetchCheck);
JPanel pic_Panel = new JPanel();
pictureLabel = new JLabel();
pictureLabel.setFont(pictureLabel.getFont().deriveFont(Font.ITALIC));
this.updatePicture();
pic_Panel.add(pictureLabel);
this.getContentPane().add(check_Panel, BorderLayout.LINE_START);
this.getContentPane().add(pic_Panel, BorderLayout.CENTER);
this.setTitle("CheckDemo");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setCenterLocation();
}
/**
* 方法说明:将窗体定义到屏幕中间
*/
private void setCenterLocation(){
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = this.getSize().width;
int h = this.getSize().height;
System.out.println("宽度: "+w+" ,高度:"+h);
int x = (dim.width - w)/2;
int y = (dim.height - h)/2;
this.setLocation(x, y);
}
/**
* 方法说明:生产图标
* @param path 图片的存放路径
* @return ImageIcon
*/
protected static ImageIcon createImageIcon(String path){
ImageIcon image = null;
try{
java.net.URL imgURL = MyButtonDemo.class.getClassLoader().getResource(path);
if(imgURL != null){
image = new ImageIcon(imgURL);
}else{
System.err.println("could not find file: "+path);
}
}catch(Exception e){
e.printStackTrace();
}
if(image != null){
return image;
}else{
return null;
}
}
/**
* 进行图片的更新
*/
public void updatePicture(){
//
ImageIcon icon = createImageIcon("base2011/images/geek/geek-"
+choices.toString()+".gif");
if(icon == null){
pictureLabel.setText("没有发现图片");
}else{
pictureLabel.setIcon(icon);
pictureLabel.setToolTipText(choices.toString());
}
}
/**
* 事件处理
*/
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
int index = 0;
char c = '-';
//选择事件发生
Object source = e.getSource();
if(source == chinCheck){
index = 0;
c = 'c';
}else if(source == glassesCheck){
index = 1;
c = 'g';
}else if(source == hairCheck){
index = 2;
c = 'h';
}else if(source == teetchCheck){
index = 3;
c = 't';
}
//取消选择事件
if(e.getStateChange() == ItemEvent.DESELECTED){
c = '-';
}
//该标识的名字
choices.setCharAt(index, c);
//更新图片
updatePicture();
}
/**
* 主方法
*/
public static void main(String[] args){
new MyCheckDemo();
}
}