问题描述
public class MyFrame extends JFrame
{
public MyFrame(String title)
{
setSize(200, 200);
setTitle(Integer.toString(super.getSize().width));
setLayout(new FlowLayout());
for (int i = 0; i < 5; ++i)
{
JButton b = new JButton();
b.setSize(90,50);
b.setText(Integer.toString(b.getSize().width));
this.add(b);![alt text][1]
}
this.setVisible(true);
}
}
为什么如果有按钮widht 90我正在获得窗口哪三个按钮在一行而不是两行?
why if having button widht 90 I'm getting window where three buttons are in one row instead of two?
推荐答案
FlowLayout
将布置组件
从左到右(或从右到左)包装它们(如果需要)。如果您希望明确设置每个 JButton
的大小,则应使用 setPreferredSize 而不是 setSize
布局经理通常在执行布局时使用最小,首选和最大尺寸。
FlowLayout
will lay out Component
s left-to-right (or right-to-left) wrapping them if required. If you wish to explicitly set the size of each JButton
you should use setPreferredSize rather than setSize
as layout managers typically make use of the minimum, preferred and maximum sizes when performing a layout.
尺寸属性非常混乱 - 有一篇有趣的文章这里一>。特别注意:
Size properties are quite confusing - There is an interesting article here. In particular, note:
某些布局管理器,例如
GridLayout,完全忽略大小
属性。
Some layout managers, such as GridLayout, completely ignore the size properties.
FlowLayout,尝试要兑现preferredSize的
维度,
可能无需兑现
minimumSize或maximumSize。
FlowLayout, attempts to honor both dimensions of preferredSize, and possibly has no need to honor either minimumSize or maximumSize.
这篇关于FlowLayout问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!