如何在单击JMenuItem时阻止JMenuItem关闭Menu [英] How to prevent JMenuItem from closing Menu upon clicking the JMenuItem

查看:267
本文介绍了如何在单击JMenuItem时阻止JMenuItem关闭Menu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在点击 JMenuItem 时关闭菜单,如何阻止 JMenuItem

How do I prevent a JMenuItem from closing the menu when the JMenuItem is clicked?

启用 JMenuItem

所以这就是场景,我有3 JMenuItems

So this is the scenario, I have 3 JMenuItems:

JMenuItem:A,B,C ;

C显示整数 X

A和B用于按值递增或递减 X 1.
如果单击A或B,则默认性质是菜单将在单击时关闭。
我希望能够重复点击A或B并保持菜单,并在每次点击时执行相关的操作。

A and B are used to increment or decrement X by a value of 1. If A or B is clicked, the default nature is that the menu will close upon click. I want to be able to repeatedly click A or B and have the menu remain up, and perform the associated 'action' upon each click.

谢谢!

推荐答案

首先,使用菜单执行此操作可能是错误的方法。 JSpinner 似乎更合适。但是,要使用 JMenuItem 执行此操作,您可以继承 LookAndFeel的 MenuItemUI 您正在使用,并覆盖 doClick(...)方法,以便 clearSelectionPath()未调用,在单击项目时关闭菜单。

First, using a menu to do this may be the wrong approach. JSpinner seems more appropriate. However, to do this with a JMenuItem you can subclass the MenuItemUI of the LookAndFeel you are using, and override the doClick(...) method so that clearSelectionPath() is not called, which closes the menu when the item is clicked.

示例,如果您使用的是Motif LookAndFeel 你可以这样做:

Example, if you are using the Motif LookAndFeel you can do this:

menuItem.setUI(new MotifMenuItemUI() {
    @Override
    protected void doClick(MenuSelectionManager msm) {
        menuItem.doClick(0);
    }
});

我自己没有试过这个,但我认为它会起作用。

I haven't ever tried this myself but I think it will work.

这篇关于如何在单击JMenuItem时阻止JMenuItem关闭Menu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆