[JAVA100例]012、复合框
时间:2011-01-08
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* <p>Title: 检查盒演示</p>
* <p>Description: 选择不同的选择框显示不同的图片</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: CheckBoxDemo.java</p>
* @version 1.0
*/
public class CheckBoxDemo extends JPanel
implements ItemListener {
JCheckBox chinButton;
JCheckBox glassesButton;
JCheckBox hairButton;
JCheckBox teethButton;
/*
* 有四个检查盒,分别对应下巴、眼镜、头发和牙齿
* 图片不是拼出来的,而是根据检查盒选择拼写图片文件名
* 图片文件名的定义格式为"geek-XXXX.gif"
* 其中 XXXX 根据检查盒的不同选择,而不同。它的格式如下:
---- //没有选择
c--- //一个选择
-g--
--h-
---t
cg-- //两个选择
c-h-
c--t
-gh-
-g-t
--ht
-ght //三个选择
c-ht
cg-t
cgh-
cght //所有都选
*/
StringBuffer choices;
JLabel pictureLabel;
public CheckBoxDemo() {
super(new BorderLayout());
//创建检查盒
chinButton = new JCheckBox("下巴(c)");
chinButton.setMnemonic(KeyEvent.VK_C);
chinButton.setSelected(true);
glassesButton = new JCheckBox("眼镜(g)");
glassesButton.setMnemonic(KeyEvent.VK_G);
glassesButton.setSelected(true);
hairButton = new JCheckBox("头发(h)");
hairButton.setMnemonic(KeyEvent.VK_H);
hairButton.setSelected(true);
teethButton = new JCheckBox("牙齿(t)");
teethButton.setMnemonic(KeyEvent.VK_T);
teethButton.setSelected(true);
//给检查盒添加监听
chinButton.addItemListener(this);
glassesButton.addItemListener(this);
hairButton.addItemListener(this);
teethButton.addItemListener(this);
choices = new StringBuffer("cght");
//放置一个带图片的标签
pictureLabel = new JLabel();
pictureLabel.setFont(pictureLabel.getFont().deriveFont(Font.ITALIC));
updatePicture();
//将检查盒放置到面版中
JPanel checkPanel = new JPanel(new GridLayout(0, 1));
checkPanel.add(chinButton);
checkPanel.add(glassesButton);
checkPanel.add(hairButton);
checkPanel.add(teethButton);
add(checkPanel, BorderLayout.LINE_START);
add(pictureLabel, BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
/**
*<br>方法说明:监听检查盒事件,拼凑图片的文件名XXXX部分
*<br>输入参数:
*<br>返回类型:
*/
public void itemStateChanged(ItemEvent e) {
int index = 0;
char c = ´-´;
Object source = e.getItemSelectable();
if (source == chinButton) {
index = 0;
c = ´c´;
} else if (source == glassesButton) {
index = 1;
c =
|