【发布时间】:2015-06-23 21:46:39
【问题描述】:
我正在尝试使用 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);
谢谢。
【问题讨论】:
标签: java user-interface jpanel gridbaglayout