如何设置JPanel大小?
我正在使用 Netbean 的表单创建器,并且正在尝试一些事情。我不确定它是否是布局管理器,但是当我创建自己的 JPanel 并将其添加到窗口的主内容窗格时,无论我使用什么重新调整尺寸方法,面板的大小始终在 FrameView 内最大化例如 setSize 或 setPreferred 大小。我对 AWT 和 Swing 还很陌生。
I'm using Netbean's form creator and I'm trying out some things. I'm not sure if it's the layout manager, but when I create my own JPanel and add it to the main content pane of the my window, the size of the panel always maximizes inside the FrameView no matter what re-dimensioning methodology I use such as setSize or setPreferred size. I'm very new to AWT and Swing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 NetBeans WYSIWYG 设计器,一切变得轻而易举 - 只需确保使用自由形式设计并使用鼠标调整大小即可。也许面板本身比 FrameView 大,所以双击它(您正在专门编辑它)并使其变小。比双击返回父组件应该没问题。
或者可以在 NetBeans 站点 上查看一些教程。
With NetBeans WYSIWYG designer it's a peace of cake - just be sure to use Free Form Design and use mouse for resizing. Maybe the panel is itself larger than FrameView, so double click on it (you are editing it exclusively) and make it smaller. Than double click to get back to parent component and you should be fine.
Or maybe check some tutorials at NetBeans site.
您不不应该手动设置尺寸。将其留给布局管理器,当窗口大小或字体甚至只是按钮标签发生变化时,您的 GUI 不会中断。
布局管理器的技巧是,您可以使用不仅仅是一个,而是多个,位于嵌套的 JPanel 中。这样,几乎任何布局都是可能的。
You're not supposed to set sizes manually. Leave that to the layout managers, and your GUI will not break when the window size or the font or even just a button label changes.
The trick with layout managers is that you can use not just one but several, in nested
JPanel
s. That way, nearly any layout is possible.发生这种情况是因为 JPanel 容器的布局管理器(可能是 JFrame 或另一个 JPanel)指示 JPanel 最大化。
执行以下操作:
找出谁是此 JPanel 的父级(查看 NetBeans 的包含对象树)
检查为容器定义了哪些布局
阅读有关 LayoutManager 的文档(它们位于 java.awt 包中)
???
利润!
This happens because the layout manager of JPanel's container (perhaps it is either JFrame or another JPanel) instructs the JPanel to maximize.
Do the following:
Find out, who is the parent of this JPanel (take a look at NetBeans's containment object tree)
Check, what layout is defined for the container
Read the documentation about LayoutManager's (they're in java.awt package)
???
PROFIT!
根据我的经验,Swing 是一个非常挑剔的东西。我会尝试
setMaximumSize
和setPreferedSize
。不过,作为旁注:当我使用 GridLayout 时,它总是拉伸每个单元格中的任何内容(我猜是为了使其对称)。 Flow、Box、Border 和我认为 GridBag 没有这个问题。-布雷特
In my experience, Swing is a very picky thing. I'd try
setMaximumSize
andsetPreferedSize
. As a side note though: when ever I usedGridLayout
, it always stretches whatever is in each of the cells (to make it symmetrical I guess). Flow, Box, Border, and I think GridBag don't have that problem though.-Brett