Java:如何将 JMenu 添加到 JPanel 或创建下拉按钮?

发布于 2024-07-16 19:49:44 字数 243 浏览 5 评论 0原文

有什么方法可以将 JMenuItem 添加到 JPanel 中,以便我可以创建一个按钮来显示多个选项,例如地址栏下的 firefox 中的最新新闻按钮?

我只能将 JMenu 和 JMenuItems 添加到 JMenuBar、JPopupMenu 和其他 JMenus 和 JMenuitems

有没有办法在 Java Swing 中创建下拉 Jbutton? (左侧带有向下箭头的,向用户显示更多选项)

提前致谢

is there any way to add a JMenuItem to a JPanel so that I can create a button to show multiple options, like the latest news button in firefox, under the address bar?

I can only add JMenu and JMenuItems to a JMenuBar, JPopupMenu and other JMenus and JMenuitems

Is there any way to create in Java Swing a drop down Jbutton? (the ones with an down arrow in their left which shows more options to the user)

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惯饮孤独 2024-07-23 19:49:44

最后,我用一个简单的 Jutton 和一个带有要显示的选项的 JPopupPane 实现了“显示选项按钮”:

在按钮的 ActionPerformedListener 中,我编写了以下代码:

popMenu.show(showOptionsButton,0,showOptionsButton.getHeight())

它工作得很好,就像 JMenuBar 中的 JMenu 一样,但不完全像下拉菜单按钮,您可以在其中按按钮执行操作或按向下箭头显示更多操作。 我相信这可以使用两个“非常接近”的按钮来完成,然后在箭头按钮中使用上面的代码,但将“操作按钮”设置为弹出窗口的组件,以便弹出窗口显示在两个按钮的下方。

Finnally i implement the "show options button" with a simple Jutton, and a JPopupPane with the options to show:

In the ActionPerformedListener of the button i write this code:

popMenu.show(showOptionsButton,0,showOptionsButton.getHeight())

It works fine like a JMenu in a JMenuBar, but not exactly like a dropdown button, in which you can perform an action pressing the button or show more actions pressing the down arrow. I believe this can be done ussing two buttons "very close", then use the code above in the arrow button, but setting the "action button" as the component of the popup, so that the popup shows below of both buttons.

巾帼英雄 2024-07-23 19:49:44

JMenuBar 只能添加到 JFrame (setMenuBar()),而不是 JPanel

Swing 下拉按钮就是此处的方法。

请参阅这篇文章,了解有关各种实现的详细讨论提议

替代文本 http://blogs.sun.com/geertjan/resource/dropdownbutton1 -jl.png

上述下拉按钮在任何发行版中使用 NetBeans UI Utilities APIplatform7/modules/org-openide-awt.jar) NetBeans IDE)。 您不需要 NetBeans 来运行它:只需从 NetBeans 安装中提取这个 jar 即可。

A JMenuBar should only be added to a JFrame (setMenuBar()), not a JPanel.

A Swing drop down button is the way to go here.

See this article for a good discussion on various implementation propositions

alt text http://blogs.sun.com/geertjan/resource/dropdownbutton1-jl.png

The above drop-down button use the NetBeans UI Utilities API (platform7/modules/org-openide-awt.jar in any distribution of NetBeans IDE). You do not need to have NetBeans to run it: only this jar you have extracted from the NetBeans installation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文