如何禁用 java swing 菜单项?
我使用 awt 制作了一个菜单,并在该菜单中填充了多个项目。如何“禁用”菜单项?
I used the awt to make a menu and populated that menu with several items. How do I "disable" the menu items?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
忍不住对其他答案抱怨一下:可以更改 menuItem 本身的启用状态,因此它们在技术上是正确的。然而,大多数时候这并不是一个好主意。
除了最简单的环境之外,管理启用状态可能会变得相当复杂。通常,需要专用的状态模型。该状态模型的目标项应该是类似数据的(而不是类似视图的)。菜单项(或其他按钮)的理想(因为它是为此设计的:)目标项是
Action
。简短版本:用
Action
填充菜单,然后管理操作而不是组件的启用。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.
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
.Short version: fill menus with
Action
s and then manage the enablement of the action instead of the component.