我正在尝试创建一个带有网格的GUI。
下面是我用一个尺寸完美的网格Glayout创建的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。