JButton 更新自己的 isEnabled 值

发布于 2024-09-13 09:01:01 字数 891 浏览 3 评论 0原文

我正在尝试创建 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 类中私有存储的实际布尔值。我的问题是:可以重写 JButton 的哪种方法来更新其存储的 isEnabled 布尔值(通过 setEnabled(boolean))?会更新(图形)吗?或重绘()?或者其他一些功能?

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

I'm trying to create a sub-class of the JButton component that will enable or disable itself based on a condition (which looks like below)

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();
}

However, the JButton code is all based on the actual boolean value stored privately in the JButton class. My question is: which method of JButton can be overridden to update its stored isEnabled boolean (via setEnabled(boolean))? Would it be update(Graphics)? or repaint()? OR some other function?

Edit: Realized that what I'm trying to create is actually impossible, unless you have a separate thread that waits short periods of time and forces the button to check its status (which is gross and I don't want to do that). The fact is, buttons are reactive only. It would be possible to accomplish this with some overhead by whoever uses the button class, but at that point it'd be easier to just write listeners on whatever is actually changing and toggle the button at that point. Woops.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空名 2024-09-20 09:01:01

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

附录:作为替代方案,如文章 Key 中所述绑定,“Actions 的优点是它们具有启用状态,这提供了一种简单的方法来禁用该操作,而无需跟踪它附加到哪个组件。”请参阅如何使用操作了解更多。

The DefaultButtonModel, for example, maintains a stateMask, which includes a bit for the enabled state. You could implement the ButtonModel interface accordingly.

Addendum: As an alternative, and as noted in the article Key Bindings, "Another advantage of Actions is that they have an enabled state which provides an easy way to disable the action without having to track which component it is attached to." See How to Use Actions for more.

染柒℉ 2024-09-20 09:01:01

我强烈建议使用 Actions 来实现这样的功能。

您应该启用/禁用附加到按钮的操作。在 Swing 中,相同的操作可以与多种类型的组件相关联,例如按钮、菜单项等。通过禁用特定操作,您将自动禁用所有关联的组件。

当您拥有具有相同操作集的工具栏、上下文菜单等时,这会变得非常方便。

I strongly suggest using Actions to implement such a feature.

You should enable/disable action attached to your button. In Swing same action can be associated with many types of components such as buttons, menu items etc. By disabling a specific action you will automatically disable all associated components.

This becomes very convenient when you have toolbars, context menus etc with the same set of actions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文