[英]Why is one of the panel in GridBagLayout expanding when i add a JLabel?
我正在尝试使用gridbaglayout创建一个GUI。
下面是我使用大小合适的gridbaglayout创建的GUI。
http://postimg.org/image/azjsw5xvd/
但是,当我尝试在“功能”面板中添加JLabel时,我的“功能”面板会自动展开并更改我的GUI。
自动扩展面板请参见下文。
http://postimg.org/image/xu1h21lk1/
如何锁定所有面板,以免改变其尺寸并将其保持在此面板比例之内?
下面是我用来使用gridbaglayout对齐GUI的源代码。 我希望在调整框架大小或将组件添加到子面板时,确保所有面板保持相同的面板比率。
frame = new JFrame("Cocoon");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
background = new JPanel();
background.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
frame.getContentPane().add(background, BorderLayout.CENTER);
filePanel = new FilesPanel();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.gridheight = 10;
gbc.weightx = 0.2;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(10, 10, 10, 10);
background.add(filePanel, gbc);
beforePanel = new BeforePanel();
afterPanel = new AfterPanel();
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth = 7;
gbc.gridheight = 8;
gbc.weightx = 0.5;
gbc.weighty = 0.8;
codes = new JSplitPane(JSplitPane.VERTICAL_SPLIT, beforePanel, afterPanel);
codes.setOneTouchExpandable(true);
codes.setResizeWeight(0.5);
background.add(codes, gbc);
feedBackPanel = new FeedbackPanel();
gbc.gridx = 2;
gbc.gridy = 8;
gbc.gridwidth = 5;
gbc.gridheight = 2;
gbc.weightx = 0.5;
gbc.weighty = 0.2;
background.add(feedBackPanel, gbc);
functionsPanel = new FunctionsPanel();
gbc.gridx = 7;
gbc.gridy = 0;
gbc.gridwidth = 3;
gbc.gridheight = 10;
gbc.weightx = 0.3;
gbc.weighty = 1.0;
background.add(functionsPanel, gbc);
frame.add(background);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.validate();
frame.pack();
frame.setVisible(true);
谢谢。
看看这个答案 。 基本上,没有简单的设置可以使GridBagLayout根据内容调整布局大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.