[英]BorderLayout doesn't honor maximumsize
I have a problem using BorderLayout
, but first of all, here is my GUI setup: 我在使用
BorderLayout
遇到问题,但首先,这是我的GUI设置:
As you can see, I have 3 different components inside my JFrame
. 如您所见,我的
JFrame
有3个不同的组件。 Adding the JMenu
and JList
works fine. 添加
JMenu
和JList
可以正常工作。 But my JPanel
should have a fixed size so I want to prevent my BorderLayout
from stretching the panel. 但是我的
JPanel
应该具有固定的大小,因此我想防止BorderLayout
拉伸面板。 I tried everything, setPreferredSize() setMinimumSize() setMaximumSize() setSize()
but again the layout stretches my panel to fit to the frame. 我尝试了一切,
setPreferredSize() setMinimumSize() setMaximumSize() setSize()
但是布局再次拉伸了我的面板以适合框架。 (The panel is added to the frame using BorderLayout.CENTER
). (使用
BorderLayout.CENTER
将面板添加到框架中)。
Is there any way to prevent this or do you have other suggestions to manage the problem? 有什么方法可以防止这种情况,或者您有其他建议来解决该问题?
I'm pretty sure you mean BorderLayout
, not BoxLayout
, because there is no BoxLayout.CENTER
and it looks like you use a BorderLayout
to place the components. 我很确定您的意思是
BorderLayout
,而不是BoxLayout
,因为没有BoxLayout.CENTER
,并且看起来您使用BorderLayout
来放置组件。
I think the problem here is that you only set the preferred size of the panel that you add to BorderLayout.CENTER
. 我认为这里的问题是,您只能设置添加到
BorderLayout.CENTER
的面板的首选大小。 This doesn't have any effect. 这没有任何作用。 Instead you need nested layouts.
相反,您需要嵌套的布局。
In this example I added the JPanel
called centerPanel
, which is using a standard GridBagLayout
(to center the added component), to BorderLayout.CENTER
. 在此示例中,我将一个名为
centerPanel
的JPanel
添加到BorderLayout.CENTER
,该JPanel
使用标准的GridBagLayout
(以使添加的组件居中)。 Then I added the additional JPanel
called panel
, which has a custom preferrdSize
, to centerPanel
. 然后,我在
preferrdSize
添加了另一个名为panel
JPanel
,它具有自定义的centerPanel
。 This way panel
won't get stretched. 这样
panel
不会被拉长。
Code: 码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
public class Example {
public Example() {
JMenuBar menuBar = new JMenuBar();
menuBar.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
DefaultListModel<String> listModel = new DefaultListModel<String>();
JList<String> list = new JList<String>(listModel);
list.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.BLACK));
JPanel panel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
// Uncomment the following lines if you also want to prevent the
// 'wrapping' of the panel.
/*
* @Override public Dimension getMinimumSize() { return new
* Dimension(400, 400); }
*/
};
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
for (int i = 1; i <= 5; i++) {
menuBar.add(new JMenu("Menu " + i));
listModel.addElement("Element " + i);
panel.add(new JLabel("Label " + i));
}
JPanel centerPanel = new JPanel(new GridBagLayout());
centerPanel.add(panel);
JPanel contentPanel = new JPanel(new BorderLayout());
contentPanel.add(menuBar, BorderLayout.NORTH);
contentPanel.add(list, BorderLayout.WEST);
contentPanel.add(centerPanel);
JFrame frame = new JFrame();
frame.setContentPane(contentPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Example();
}
});
}
}
Add your existing JPanel
to a JPanel
having Flowlayout
, the default, or having GridBagLayout
with default constraints. 将现有的
JPanel
添加到具有Flowlayout
,默认值或具有默认约束的GridBagLayout
的JPanel
中。 Add this panel to the frame's center, BorderLayout.CENTER
by default. 默认情况下,将此面板添加到框架的中心
BorderLayout.CENTER
。
Panel centerPane = new Panel(new GridBagLayout())`;
centerPane.add(yourJPanel);
frame.add(centerPane, BorderLayout.CENTER);
Also consider using Box
, rather than a JPanel
having BoxLayout
. 还可以考虑使用
Box
,而不是使用具有BoxLayout
的JPanel
。
Also consider using the frame's setJMenuBar()
, rather than add(BorderLayout.PAGE_START)
. 还可以考虑使用框架的
setJMenuBar()
,而不要使用add(BorderLayout.PAGE_START)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.