简体   繁体   English

如何设置JPanel大小?

[英]How to set JPanel size?

I'm using Netbean's form creator and I'm trying out some things. 我正在使用Netbean的表单创建器,并且正在尝试一些操作。 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. 我不确定是否是布局管理器,但是当我创建自己的JPanel并将其添加到窗口的主内容窗格时,无论我使用哪种重新定义尺寸的方法,面板的大小始终在FrameView中最大化例如setSize或setPreferred大小。 I'm very new to AWT and Swing. 我是AWT和Swing的新手。

With NetBeans WYSIWYG designer it's a peace of cake - just be sure to use Free Form Design and use mouse for resizing. 使用NetBeans所见即所得的设计器,您可以放心使用-只需确保使用Free Form Design并使用鼠标来调整大小即可。 Maybe the panel is itself larger than FrameView, so double click on it (you are editing it exclusively) and make it smaller. 也许面板本身比FrameView大,所以双击它(仅在编辑时)并缩小它。 Than double click to get back to parent component and you should be fine. 比双击返回父组件要好。

Or maybe check some tutorials at NetBeans site . 或者,可以在NetBeans网站上查看一些教程。

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. 将其留给布局管理器,并且当窗口大小或字体甚至按钮标签更改时,GUI不会中断。

The trick with layout managers is that you can use not just one but several, in nested JPanel s. 布局管理器的窍门是,在嵌套的JPanel ,您不仅可以使用一个,而且可以使用多个。 That way, nearly any layout is possible. 这样,几乎任何布局都是可能的。

This happens because the layout manager of JPanel's container (perhaps it is either JFrame or another JPanel) instructs the JPanel to maximize. 发生这种情况是因为JPanel容器的布局管理器(也许是JFrame或另一个JPanel)指示JPanel最大化。

Do the following: 请执行下列操作:

  1. Find out, who is the parent of this JPanel (take a look at NetBeans's containment object tree) 找出谁是此JPanel的父级(看看NetBeans的包含对象树)

  2. Check, what layout is defined for the container 检查为容器定义了什么布局

  3. Read the documentation about LayoutManager's (they're in java.awt package) 阅读有关LayoutManager的文档(它们位于java.awt包中)

  4. ??? ???

  5. PROFIT! 利润!

In my experience, Swing is a very picky thing. 以我的经验,Swing是非常挑剔的事情。 I'd try setMaximumSize and setPreferedSize . 我会尝试setMaximumSizesetPreferedSize As a side note though: when ever I used GridLayout , it always stretches whatever is in each of the cells (to make it symmetrical I guess). 但要注意的是:每当我使用GridLayout ,它总是会拉伸每个单元格中的内容(我想使其对称)。 Flow, Box, Border, and I think GridBag don't have that problem though. Flow,Box,Border和我认为GridBag都没有这个问题。

-Brett -布雷特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM