如何在对话框顶部布局一个面板,使其具有两个按钮?
我需要在对话框顶部布局一个面板,以便它有两个按钮(保存和取消)。
我希望保存位于左侧,取消位于右侧。
我已经使用 MigLayout 创建了一个 JPanel,并将其停靠在内容窗格的北部,但我一生都无法弄清楚如何向其中添加两个按钮,以便它们按照我想要的方式显示。 将它们停靠在面板中似乎消除了对话框中的所有填充(这看起来很糟糕)。
任何帮助将不胜感激。
I need to layout a panel on the top of my Dialog so that it has two buttons (Save and Cancel).
I want the save to be on the left and Cancel to be on the right side.
I've created a JPanel using the MigLayout and docked it to the north of the content pane, and can't for the life of me figure out how to add the two buttons to it so that they appear as I want them. Docking them within the panel seems to get rid of all padding in the dialog (which looks terrible).
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
顺便说一句,您可能不应该指定哪个按钮位于左侧或右侧。 这是 MiGLayout 的酷炫之处之一(平台独立性,甚至在诸如取消按钮应该放在哪里之类的事情上)。
现在,按钮将根据平台以正确的顺序显示。
这是一篇文章,其中的代码可以实现您想要的功能。 我强烈建议避免尝试强制按钮等组件的大小(这些实际上应该来自平台的外观和感觉)。 另外,如果这样做有意义的话,对接也是可以的,但我很少发现有必要这样做。 无需为按钮构建完全独立的面板,只需跨越包含按钮的行即可 - 更加干净,并且您不会最终得到所有嵌套面板。
很难摆脱嵌套面板的边框布局技术,但一旦掌握了它,MigLayout 就是一个梦想。 顺便说一句 - 我知道有时您可能希望在库中构建按钮面板 - 如果是这种情况,那么单独的面板可能是有意义的(尽管您也可以让库向现有面板添加按钮行,而不是返回一个面板,然后将其添加到布局中)。
As an aside, you should probably not be dictating which button is on the left or right. That's one of the way cool things about MiGLayout (platform independence, even on things such as where the cancel button should go).
Now the buttons will appear in the correct order, based on platform.
Here's an article with code doing what you are going for. I strongly recommend avoiding trying to force the size of components like buttons (these really should come from the platform look and feel). Also, docking is fine if it makes sense to do so, but I rarely find it to be necessary. Instead of building a totally separate panel for your buttons, just span the row that contains the buttons - much cleaner, and you don't wind up with all of the nested panels.
It's hard to break from the border layout technique of nested panels, but once you get the hang of it, MigLayout is a dream. BTW - I understand that there are times where you might want to build up the button panel in a library - if that's the case, then separate panels may make sense (although you could also have the library add a button row to an existing panel, instead of returning a panel that you then add to the layout).
哦,每当你提出问题时,答案总是会弹出:
请注意,内容窗格也使用 MigLayout。
Doh, always happens as soon as you ask a question, the answer pops out:
Note that the content pane is using the MigLayout too.