如何禁用 java swing 菜单项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5672917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:12:50  来源:igfitidea点击:

How do I disable java swing menu items?

javaswingawt

提问by kleopatra

I used the awt to make a menu and populated that menu with several items. How do I "disable" the menu items?

我使用 awt 制作了一个菜单,并用几个项目填充了该菜单。如何“禁用”菜单项?

回答by Prince John Wesley

   menuItemInstance.setEnabled(false);

回答by kleopatra

can't resist to grumble a bit about the other answers: changing enabled state of the menuItem itself can be done, so they are technically correct. Nevertheless, it's not a good idea most of the time.

忍不住对其他答案发牢骚:可以更改 menuItem 本身的启用状态,因此它们在技术上是正确的。然而,大多数时候这不是一个好主意。

Managing enablement state can get quite complex in anything but the most simple environments. Typically, a dedicated state model is needed. Target items of that state model should be data-like (as opposed to view-like). The ideal (because it was designed for it :) target item for menuItems (or other buttons) is the Action.

除了最简单的环境之外,管理启用状态在任何情况下都会变得非常复杂。通常,需要专用的状态模型。该状态模型的目标项应该是类似数据的(而不是类似视图)。menuItems(或其他按钮)的理想(因为它是为它设计的:)目标项目是Action.

Short version: fill menus with Actions and then manage the enablement of the action instead of the component.

简短版本:用Actions填充菜单,然后管理操作而不是组件的启用。