本文主要是介绍swing学习笔记十四(复选框JCheckBox ),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*** 复选框
*
* @time 3:54:18 PM
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class Test_JcheckBoxTest extends JApplet implements ItemListener {
private static final long serialVersionUID = 1L;
int sum = 0, a = 0, b = 0, c = 0, d = 0;
// 取得当前容器
Container container = getContentPane();
JTextField textField = new JTextField(10);
JLabel label = new JLabel("总价:");
// 复选框
JCheckBox cb1, cb2, cb3, cb4;
JButton btn = new JButton("清除");
/**
* 初始化小程序
*/
public void init() {
container.setLayout(new FlowLayout());
cb1 = new JCheckBox("显示器1350", false);
cb1.addItemListener(this);
container.add(cb1);
cb2 = new JCheckBox("内存780", false);
cb2.addItemListener(this);
container.add(cb2);
cb3 = new JCheckBox("硬盘1100", false);
cb3.addItemListener(this);
container.add(cb3);
cb4 = new JCheckBox("CPU410", false);
cb4.addItemListener(this);
container.add(cb4);
container.add(label);
container.add(textField);
container.add(btn);
btn.addActionListener(new koListener());
}
/**
* 复选项
*/
public void itemStateChanged(ItemEvent e) {
JCheckBox box = (JCheckBox) e.getItem();
if (box == cb1)
a = 1350;
if (box == cb2)
b = 780;
if (box == cb3)
c = 1100;
if (box == cb4)
d = 410;
sum = a + b + c + d;
textField.setText(String.valueOf(sum) + "元");
}
/**
* 清零重新计算
*/
class koListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
cb1.setSelected(false);
cb2.setSelected(false);
cb3.setSelected(false);
cb4.setSelected(false);
a = 0;
b = 0;
c = 0;
d = 0;
sum = a + b + c + d;
textField.setText(String.valueOf(sum) + "元");
}
}
}
这篇关于swing学习笔记十四(复选框JCheckBox )的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!