Java 摆动面板尺寸
大家好,我一直在学习 Java Swing 来创建国际象棋游戏,以练习我的 Java 编程技能。
我使用 BorderLayout
在 JFrame 的东边添加了一个 JPanel,并使用 setPrefferedSize(new Dimension(x,y))
方法来设置宽度和高度。
之后,我创建了 4 个 JPanel,并将它们与 BoxLayout
添加到之前创建的面板上。
我尝试使用 setSize(x,y)
和 setPreferredSize(new Dimension(x,y))
设置 4 个面板的大小,但它对 4 个面板不起作用面板会自动更改其大小以适合主 JPanel,并且在其中一个面板上添加 JLabel 后,其大小会自动增加。
这是我的代码:
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
JPanel a = new JPanel();
a.setPreferredSize(new Dimension(50, 50)); //this dosent work
a.add(min);
a.setBackground(Color.red);
this.add;
JPanel b = new JPanel();
b.setBackground(Color.blue);
this.add(b);
JPanel c = new JPanel();
this.add(c);
JPanel d = new JPanel();
d.setBackground(Color.black);
this.add(d);
如何更改每个面板的大小?
Hi I have been learning Java Swing for creating a chess game to practice my Java programming skills.
I've added a JPanel to the east of the JFrame with BorderLayout
and I've used the setPrefferedSize(new Dimension(x,y))
method to set the width and height.
After that I have created 4 JPanel and added them with BoxLayout
on the previously created panel.
I have tried to set the size of the 4 panels with the setSize(x,y)
and setPreferredSize(new Dimension(x,y))
but it dosent work the 4 panels automaticly changed there size to fit the main JPanel and after adding a JLabel on one of them the size of it increased automaticly .
This is my code:
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
JPanel a = new JPanel();
a.setPreferredSize(new Dimension(50, 50)); //this dosent work
a.add(min);
a.setBackground(Color.red);
this.add;
JPanel b = new JPanel();
b.setBackground(Color.blue);
this.add(b);
JPanel c = new JPanel();
this.add(c);
JPanel d = new JPanel();
d.setBackground(Color.black);
this.add(d);
How can I change the size of each of these panels?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
BoxLayout 最适合沿单个轴布置不同尺寸的组件。来自 Javadocs:
“BoxLayout 尝试按照组件的首选宽度(对于水平布局)或高度(对于垂直布局)来排列组件。”
这个想法是它们可能有不同的高度(对于水平布局)并且它将采用最大高度。而且,它们肯定可能有不同的宽度。此外,BoxLayout 还可以与一些,呃,“有趣”的填充块一起使用,例如 Box.createHorizontalGlue()。一旦您掌握了它的窍门,这些实际上对于灵活的、可调整大小的布局非常有用。但是,总而言之,BoxLayout 适用于不同大小的项目的灵活、可调整大小的布局。
对于更简单的情况,特别是如果您希望首选宽度和首选高度都得到“尊重”,请按照其他人的建议使用 GridLayout。
BoxLayout is best for laying out components with varying sizes along a single axis. From the Javadocs:
"BoxLayout attempts to arrange components at their preferred widths (for horizontal layout) or heights (for vertical layout)."
The idea is that they may have different heights (for a horizontal layout) and it will take the maximum height. And, they definitely may have different widths. Also, BoxLayout works with some, er, "interesting" filler pieces like
Box.createHorizontalGlue().
These are actually quite useful for flexible, resizeable layouts once you get the hang of it. But, all in all, BoxLayout is for flexible, resizable layout of items with differing sizes.For simpler cases, especially if you want both preferred width and preferred height to be "respected", use GridLayout as everybody else has suggested.