本文介绍了如何使JMenu在JMenuBar中具有Button行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让JMenu表现得像JButton,但我遇到了一些问题,希望有人可以提供帮助!

I was trying to make a JMenu behave like a JButton but I'm having some problems and hopefully someone here can help!

我添加了一个MenuListener JMenu项目有这个,但我不能让弹出菜单/焦点离开,让我能够正确点击JMenu重复次数来触发这个功能,我希望有人可以告诉我我做错了什么。谢谢。

I've added a MenuListener to the JMenu item with this but I cant get the popup menu/focus to leave to enable me to properly click the JMenu repeated times to trigger this function and i was hoping someone could tell me what i'm doing wrong. Thanks.

public void menuSelected(MenuEvent e)
        {
            ... // do stuff here code
            JMenu source = (JMenu)e.getSource();
            source.setSelected(false);
            source.setPopupMenuVisible(false);

        }


推荐答案

不完全确定你在问什么...

Not completely sure what you're asking...

JMenuBar 继承自 Container - 如果你宁愿为它添加一个 JButton 而不是 JMenu 你可以简单地调用 -

But JMenuBar inherits from Container - if you'd rather add a JButton to it than a JMenu you can simply call -

JMenuBar menuBar = ....
JButton myButton = ....
menuBar.add(myButton);

这篇关于如何使JMenu在JMenuBar中具有Button行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:16