JAVA JCKECKBOX 事件 g.addItemListener(new JCheckboxListener());

ad5248 2010-04-18 08:49:43
g.addItemListener(new JCheckboxListener());
g_1.addItemListener(new JCheckboxListener());
g_2.addItemListener(new JCheckboxListener());
g_3.addItemListener(new JCheckboxListener());
这样写事件
class JCheckboxListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
if (g.isSelected()) {/* button事件 */}
if (g_1.isSelected()) { /*button事件 */}
if (g_2.isSelected()) {/*button事件*/ }
if (g_3.isSelected()) {/*button事件*/ }
}
//为什么我只要把他们选中一次 即使下次把选中的取消了 那些button事件还在 很是疑惑 高人回答
...全文
318 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ad5248 2010-04-18
  • 打赏
  • 举报
回复
private void AddactionListener(){
g.addItemListener(new JCheckboxListener());
g_1.addItemListener(new JCheckboxListener());
g_2.addItemListener(new JCheckboxListener());
g_3.addItemListener(new JCheckboxListener());
gatesensor.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(gatesensor.isSelected()){
isnotify = true;
sensorTriggered("The Sensor Statement: ON");
}
else{
isnotify = false;
sensorTriggered("The Sensor Statement: OFF");
}
}

});

}

class JCheckboxListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
if (g.isSelected()) {
on.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!G1.isLockStatement()) {
G1.setLockStatement(true);
G1.setDoorStatement(false);
lab.setText("Statement:[locked & closed ]");
} else {
JOptionPane.showMessageDialog(null,
"THE GARAGE GATE IS LOCKED !");
}
}
});
off.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (G1.isLockStatement()) {
G1.setLockStatement(false);
G1.setDoorStatement(true);
lab.setText("Statement:[unlocked & opened ]");
} else {
JOptionPane.showMessageDialog(null,
"THE GARAGE GATE IS UNLOCKED !");
}
}
});
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (G1.isDoorStatement()) {

JOptionPane.showMessageDialog(null,
"THE GARAGE GATE IS OPENED!!");

} else {
if (G1.isLockStatement()) {
JOptionPane
.showMessageDialog(null,
"when you open the gate, unlocked frist!");
} else {
G1.setLockStatement(false);
G1.setDoorStatement(true);
lab.setText("Statement:[unlocked & opened ]");
}
}
}

});
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!G1.isDoorStatement()) {
JOptionPane.showMessageDialog(null,
"THE GARAGE GATE IS CLOSED !!");

} else {
G1.setDoorStatement(true);
G1.setLockStatement(false);
lab.setText("Statement:[locked & closed ]");
}
}
}

);
}
if (g_1.isSelected()) {
on.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!G2.isLockStatement()) {
if(G2.isDoorStatement()){
JOptionPane.showMessageDialog(null,
"when locked the door , frist close the door!");
}
else{
G2.setLockStatement(true);
lab_1.setText("Statement:[locked & closed ]");
}
} else {
JOptionPane.showMessageDialog(null,
"THE GATE one IS LOCKED !");
}
}
});
off.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (G2.isLockStatement()) {
G2.setLockStatement(false);
lab_1.setText("Statement:[unlocked & closed ]");
} else {
JOptionPane.showMessageDialog(null,
"THE GATE one IS UNLOCKED !");
}
}
});
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (G2.isDoorStatement()) {

JOptionPane.showMessageDialog(null,
"THE GATE one IS OPENED !!");

} else {
if (G2.isLockStatement()) {
JOptionPane
.showMessageDialog(null,
"when you open the door , unlocked frist!");
} else {
G2.setDoorStatement(true);
lab_1.setText("Statement:[unlocked & opened ]");
}
}
}

});
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!G2.isDoorStatement()) {
JOptionPane.showMessageDialog(null,
"THE GATE one IS CLOSED !!");

} else {
G2.setDoorStatement(false);
lab_1.setText("Statement:[unlocked & closed ]");
}
}
}

);
}
if (g_2.isSelected()) {
on.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!G3.isLockStatement()) {
if(G3.isDoorStatement()){
JOptionPane.showMessageDialog(null,
"when locked the door , frist close the door!");
}
else{
G3.setLockStatement(true);
lab_2.setText("Statement:[locked & closed ]");
}
} else {
JOptionPane.showMessageDialog(null,
"THE GATE two IS LOCKED !");
}
}
});
off.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (G3.isLockStatement()) {
G3.setLockStatement(false);
lab_2.setText("Statement:[unlocked & closed ]");
} else {
JOptionPane.showMessageDialog(null,
"THE GATE two IS UNLOCKED !");
}
}
});
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (G3.isDoorStatement()) {

JOptionPane.showMessageDialog(null,
"THE GATE two IS OPENED !!");

} else {
if (G3.isLockStatement()) {
JOptionPane
.showMessageDialog(null,
"when you open the door , unlocked frist!");
} else {
G3.setDoorStatement(true);
lab_2.setText("Statement:[unlocked & opened ]");
}
}
}

});
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!G3.isDoorStatement()) {
JOptionPane.showMessageDialog(null,
"THE two GATE IS CLOSED!");

} else {
G3.setDoorStatement(false);
lab_2.setText("Statement:[unlocked & closed ]");
}
}
}

);
}
if (g_3.isSelected()) {
on.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!G4.isLockStatement()) {
if(G4.isDoorStatement()){
JOptionPane.showMessageDialog(null,
"when locked the door ,frist close the door!");
}
else{
G4.setLockStatement(true);
lab_3.setText("Statement:[locked & closed ]");
}
} else {
JOptionPane.showMessageDialog(null,
"THE GATE three IS LOCKED !");
}
}
});
off.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (G4.isLockStatement()) {
G4.setLockStatement(false);
lab_3.setText("Statement:[unlocked & closed ]");
} else {
JOptionPane.showMessageDialog(null,
"THE GATE three IS UNLOCKED !");
}
}
});
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (G4.isDoorStatement()) {

JOptionPane.showMessageDialog(null,
"THE GATE three IS OPENED!");

} else {
if (G4.isLockStatement()) {
JOptionPane
.showMessageDialog(null,
"when you open the door , unlocked frist!");
} else {
G4.setDoorStatement(true);
lab_3.setText("Statement:[unlocked & opened ]");
}
}
}

});
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!G4.isDoorStatement()) {
JOptionPane.showMessageDialog(null,
"THE GATE three IS CLOSED!");

} else {
G4.setDoorStatement(false);
lab_3.setText("Statement:[unlocked & closed ]");
}
}
}

);
}
else{
return;
}
}
}


@Override
public void sensorTriggered(String message) {
// TODO Auto-generated method stub
if(isnotify){
lab_5.setForeground(Color.RED);
JOptionPane.showMessageDialog(null,
"Sensor Start runing!");
}
else{
lab_5.setForeground(Color.YELLOW);
JOptionPane.showMessageDialog(null,
"Sensor end runing!");
}
lab_5.setText(message);

}
}
//第一次选取都正常 但是选取一次后就不正常了 好
justchenjie 2010-04-18
  • 打赏
  • 举报
回复
这个应该是逻辑的问题吧。你的那些button事件相同吗?还有就是if之后的默认处理else也加上试试
代码下载地址: https://pan.quark.cn/s/a4b39357ea24 图书馆系统非常适合运用C++面向对象的特性进行建模。图书馆管理系统主要由四个关键模块构成:图书借阅、图书归还、图书维护以及读者服务。在系统设计中,可以定义一个读者类(Reader),用于存储每位读者的详细资料;读者数据库类(Rdatabase),用于管理所有读者的信息;图书类(Book),用于记录每本图书的基本属性;图书数据库类(Bdatabase),用于维护所有图书的记录。 【图书馆管理系统构建】 基于C++面向对象编程的图书馆管理系统,其核心功能划分为四个主要部分:图书借阅、图书归还、图书维护和读者服务。该系统通过设计多种类来模拟图书馆的实际运作,包括读者类(Reader)、读者数据库类(Rdatabase)、图书类(Book)以及图书数据库类(Bdatabase)。 1. **读者类(Reader)**: - 该类包含读者的基础资料,例如删除标记(tag)、读者编号(no)、姓名(name)以及所借图书列表(borbook)。 - 通过构造函数对读者信息进行初始化。 - 拷贝构造函数用于复制读者的姓名信息。 - 提供一系列成员函数,以支持信息的获取和设置操作。 2. **读者数据库类(Rdatabase)**: - 包含一个读者记录数组(read),并使用记录指针(top)来标识最新添加的读者信息。 - 构造函数从read.txt文件中加载所有读者数据,并在析构函数中将未删除的记录保存回文件。 - 提供管理读者信息的接口,例如添加、删除和查找功能。 3. **图书类(Book)**: - 该类存储图书的基本属性,包括删除标记、图书编号、书名(name)以及图书的在架状态...

62,622

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧