gpt4 book ai didi

java - 如何禁用桌面应用程序中的菜单项?

转载 作者:行者123 更新时间:2023-11-29 09:49:30 27 4
gpt4 key购买 nike

我在菜单栏中创建了一个菜单,我想在其中创建一个 JCheckBoxMenuItem 来设置突出显示剩余菜单项的条件。

类似于下面的伪代码:

if login(true)
then highlight remaining menuitems
else
un-highlight the menuitems

最佳答案

我认为突出显示是指启用/禁用 JMenuItem。这是可能的。

使用setEnabled:

JMenuItem item;
item.setEnabled(false); //to disable

正如 kleopatra 所建议的那样,最好的方法是为每个 JMenuItem 实现您自己的操作,并让您的操作根据状态启用/禁用按钮:

例如:

public class AMenuAction extends AbstractAction {

@override
public void actionPerformed(ActionEvent e) {
//implement your action behavior here
}

}

然后用这样的操作构造你的 JMenuItem:

AMenuAction afterLoginAction = new AMenuAction();
JMenuItem item = new JMenuItem(afterLoginAction );

当用户登录/注销时,对所需的操作调用 setEnabled 方法。

void Login()
{
afterLoginAction.setEnabled(true);
}

关于java - 如何禁用桌面应用程序中的菜单项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12130156/

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