将可点击、触发操作的 JMenuItem 直接添加到 JMenuBar?
有没有办法将 JMenuItem (或类似的按钮类型对象)添加到 JMenuBar ?
添加 JMenuItem 与 JMenuBar 的布局不能很好地配合,并且按钮看起来太像按钮了。
我们应该调整按钮以使其看起来像 JMenuItem,还是调整 JMenuBar 以正确显示 JMenuItem?或者完全是别的什么?
Is there a way to add a JMenuItem (or similar button-type object) to a JMenuBar?
Adding a JMenuItem doesn't play well with the layout of a JMenuBar, and buttons look too button-like.
Should we be tweaking the button to look like a JMenuItem or tweaking the JMenuBar to display the JMenuItem correctly? Or something else altogether?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
以下代码实现了 camickr 的解决方案,尽管在看到
JMenuItem
在JMenuBar
中呈现的默认方式后,我会想出同样的方法。它看起来相当真实,并且响应点击,但不响应助记符。我尝试提供 JMenuItems 加速器(请参阅代码),这有效,但看起来很奇怪。
The following code implements camickr's solution, although I would have come up with the same thing after seeing the default way
JMenuItem
s are rendered in aJMenuBar
. It looks reasonably authentic and responds to clicks, but not to the mnemonic.I tried giving the JMenuItems accelerators (see code) and that works but that looks really weird.
菜单栏使用 BoxLayout,它将尝试将组件拉伸到其最大尺寸。尝试使用:
如果您需要更多帮助,请发布您的 SSCCE 显示问题。
A menubar use a BoxLayout which will try to strech the component to its maximum size. Try using:
If you need more help post your SSCCE showing the problem.
只需调整 JButton 即可。
setContentAreaFilled
使其透明,setBorderPainted
消除边框,setFocusable
消除了文本周围的小边框。Just tweak the JButton.
setContentAreaFilled
makes it see-through,setBorderPainted
gets rid of the border,setFocusable
gets rid of the tiny border around the text.也许您忘记了 JMenu。您需要将 JMenuItem 放入 JMenu 中,然后将 JMenu 添加到 JMenuBar。
要构建菜单栏,您需要执行如下操作:
Maybe you're forgetting your JMenu. You need to put the JMenuItem in a JMenu, then you add the JMenu to the JMenuBar.
To build a menu bar you need to do something like the following:
我最近发生了类似的事情,我有一个 JMenuBar,其中只有 2 个 JMenuItems(所以请注意,我还没有在混合的 JMenu 和 JMenuItem 环境中尝试过这个。
首先,我最终将布局更改为左对齐的 FlowLayout ,但这在组件之间留下了太多的空间,我尝试做各种事情,但非常不满意,我最终所做的只是使用 JMenu,但覆盖了它的一些行为,以便它假装这样做。是一个 JMenuItem。
I had something like this happen recently I had a JMenuBar that only had 2 JMenuItems in (so note that I haven't tried this in a mixed JMenu and JMenuItem environment.
At first I ended up changing the Layout to a FlowLayout with a left alignment, but that left too much space in-between the components. I messed around with trying to do various things, but was very unsatisfied. What I ended up doing was just using a JMenu, but overriding some of it's behaviors so that it pretended to be a JMenuItem. Like so:
是的。或者用简单的方法来做
它会创建一个类文件,但是嗯。
Yup. Or do it the easy way
It creates a class file, but meh.
要使按钮看起来像 JMenu,只需添加翻转效果并删除按钮的边框(例如,请参见下面的代码)
所需的导入
代码
编辑
有一种改进的方法可以做到这一点
To get the button to look like a JMenu just add a rollover effect and remove the border of the button (See code below for example)
Required imports
Code
Edit
There is a much improved way to do this
让 UI 和代码都看起来不错需要一段时间。我们最终将鼠标适配器附加到 JMenu 的底层组件:
我想我们也会添加一个
KeyAdapter
,但目前还没有。Getting the UI and the code both to look good took a while. We ended up attaching a mouse adapter to the JMenu's underlying component:
I guess we'll add a
KeyAdapter
as well, but haven't yet.