Java swing:如何对齐行和列中的菜单项?
创建菜单栏及其项目时我没有遇到任何问题。但是现在,当我遇到一个问题如何使菜单项显示为列和菜单项时行状的桌子形状,我真的不知道。
目标是使用 java 创建此类菜单项。 检查此链接。
现在,我只是认为我应该使用 jpanel 作为菜单项,然后应用流程布局,然后添加许多 jlabel,就像我可以作为网格内的菜单项一样。但这不是最糟糕的吗? 创建菜单项(例如上述链接上的图像预览)的最佳方案是什么?
我尝试google了一下,没有发现相关案例。 CMIIW。
I'm have no trouble when creating menu bar and its item. But now, when I get a question how to make the menu items appeared as column & rows-like table shaped, I really don't know about that.
The goals is to create this kind of menu items using java.
Check this link.
And right now, I just think that I should use a jpanel as menu item, and then applying a flowlayout and then adding many jlabel(s) as I could as menuitem inside the grid. But wouldn't it worst?
What's the best deal to create the menu items such as the image preview on the above links?
I tried google, but found no related cases. CMIIW.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JMenu
实例的弹出菜单是一个标准容器,因此您可以向其中添加任何您想要的内容。它有默认布局,但您可以更改它。您的模型中的类似内容是由以下代码创建的:
The popup menu of a
JMenu
instance is a standard container, so you can add to it whatever you want. It has a default layout, but you can change it.Something like in your mockup is created by this code:
我还没有看到像这样的现成组件。所以我认为你只能靠自己了。
我看到两种可能性:
JMenuItem 是一个 JComponent,因此您可以向其中添加其他组件。您可能想使用某种基于网格的布局并为数字添加按钮或标签。
实现您自己的 JMenuItem 来显示您的网格组件而不是普通的 JPopupMenu
无论如何,请查看 JMenu(Item) 的源代码以了解这些组件的工作原理。
I haven't seen a ready made component for anything like this. So I think you are on your own.
I see two possibilities:
JMenuItem is a JComponent, so you can add other components to it. You probably want to use some kind of grid based layout and add buttons or labels for the numbers.
Implement you own JMenuItem that displays your grid component instead of the normal JPopupMenu
In any case have a look at the source code of JMenu(Item) in order to understand how these components work.
最简单的解决方案就是设置 JMenu 的 JPopupMenu 的布局,然后像平常一样添加项目。无需创建子类。
例子:
The simplest solution is just set the layout of JMenu's JPopupMenu and then add items like you normally would. There's no need to create a subclass.
Example: