gpt4 book ai didi

java - JButton 更新自己的 isEnabled 值

转载 作者:行者123 更新时间:2023-11-30 05:10:45 24 4
gpt4 key购买 nike

我正在尝试创建 JButton 组件的子类,该子类将根据条件启用或禁用自身(如下所示)

public interface Condition {
public static final Condition TRUE = new Condition() {
public boolean test() {
return true;
} };
public static final Condition FALSE = new Condition() {
public boolean test() {
return false;
} };
public boolean test();
}

但是,JButton 代码都是基于 JButton 类中私有(private)存储的实际 boolean 值。我的问题是:可以重写 JButton 的哪种方法来更新其存储的 isEnabled boolean 值(通过 setEnabled(boolean))?会更新(图形)吗?或重绘()?或者其他一些功能?

编辑:意识到我想要创建的实际上是不可能的,除非你有一个单独的线程等待较短的时间并强制按钮检查其状态(这很恶心,我不想这样做那)。事实上,按钮只是 react 性的。无论谁使用按钮类,都可以通过一些开销来完成此任务,但此时,只需在实际发生更改的内容上编写监听器并在此时切换按钮会更容易。糟糕。

最佳答案

DefaultButtonModel例如,维护一个stateMask,其中包括一个用于启用状态的位。您可以相应地实现 ButtonModel 接口(interface)。

附录:作为替代方案,如文章 Key Bindings 中所述,“Actions 的另一个优点是它们具有启用状态,这提供了一种禁用操作的简单方法,而无需跟踪它所连接的组件。”请参阅How to Use Actions了解更多。

关于java - JButton 更新自己的 isEnabled 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3443055/

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