作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 JButtons“暂停”和“取消暂停”。当用户暂停程序时,应禁用“暂停”按钮,并启用“取消暂停”按钮。我不知道怎么写。取消暂停按钮有效,但暂停按钮不起作用,因为“无法解决取消暂停”。怎么处理呢?这是我的代码:
final JButton pause = new JButton("Pause");
pause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Url.pauseThread();
pause.setEnabled(false); //this works
unpause.setEnabled(true); //this does NOT work - "not resolved"
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
});
final JButton unpause = new JButton("Unpause");
unpause.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Url.resumeThread();
pause.setEnabled(true); // this works
unpause.setEnabled(false); // this works
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
});
最佳答案
声明暂停和取消暂停按钮,然后添加监听器。
final JButton pause = new JButton("Pause");
final JButton unpause = new JButton("Unpause");
pause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Url.pauseThread();
pause.setEnabled(false); //this works
unpause.setEnabled(true); //this does NOT work - "not resolved"
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
});
unpause.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Url.resumeThread();
pause.setEnabled(true); // this works
unpause.setEnabled(false); // this works
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
});
基本上,在声明取消暂停按钮之前,您就从暂停按钮监听器中的取消暂停按钮调用 setEnabled
。
关于java - 通过另一个 JButton 启用/禁用一个 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18368674/
我是一名优秀的程序员,十分优秀!