gpt4 book ai didi

java - 通过另一个 JButton 启用/禁用一个 JButton

转载 作者:行者123 更新时间:2023-12-02 06:48:09 30 4
gpt4 key购买 nike

我有 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com