我正在为学校的一个项目开发 GUI。我在摇摆中使用 GridBagLayout。
我想要一个指示输入的标签(一种文件@ x = 0,y = 0),然后是另一个标签(选择后的实际文件名@ x = 1, y = 0
),然后是文件选择器的浏览按钮(@ x = 2, y = 0
) . 处的标签(1,0)
最初是空白的,但是当标签不包含文本时,我希望文本将占据的区域占用一些空间。我还希望标签 at(0,0)
和按钮 at之间的空间(2,0)
保持不变。
为此,我尝试将标签放在面板上,然后使用布局。但是我无法缝合以达到预期的效果。有人可以提供一些建议吗?GridBagLayout 的接下来三行将以完全相同的方式布局。
这是指向 GUI 屏幕截图的链接。
calibrationFileSelectionValueLabel = new JLabel("",Label.LEFT);
calibrationFileSelectionValueLabel.setName("calibrationFileSelection");
calibrationFileSelectionValueLabel.setMinimumSize(new Dimension(100,0));
calibrationFileSelectionValuePanel = new JPanel();
calibrationFileSelectionValuePanel.setBorder(BorderFactory.createEtchedBorder());
calibrationFileSelectionValuePanel.add(calibrationFileSelectionValueLabel);
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.NONE;
filesPanel.add(calibrationFileLabel,c);
c.gridy = 1;
filesPanel.add(frequencyFileLabel,c);
c.gridy = 2;
filesPanel.add(sampleFileLabel,c);
c.gridy = 3;
filesPanel.add(outputFileLabel,c);
c.gridx = 1;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
// filesPanel.add(calibrationFileSelection,c);
filesPanel.add(calibrationFileSelectionValuePanel,c);
c.gridy = 1;
// filesPanel.add(frequencyFileSelection,c);
filesPanel.add(frequencyFileSelectionValueLabel,c);
c.gridy = 2;
// filesPanel.add(sampleFileSelection,c);
filesPanel.add(sampleFileSelectionValueLabel,c);
c.gridy = 3;
// filesPanel.add(outputFileSelection,c);
filesPanel.add(outputFileSelectionValueLabel,c);
c.gridx = 2;
c.gridy = 0;
c.fill = GridBagConstraints.NONE;
filesPanel.add(calibrationFileSelectionButton,c);
c.gridy = 1;
filesPanel.add(frequencyFileSelectionButton,c);
c.gridy = 2;
filesPanel.add(sampleFileSelectionButton,c);
c.gridy = 3;
filesPanel.add(createOutputFileButton,c);
panelForFilesPanelBorder = new JPanel();
panelForFilesPanelBorder.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(5,10,5,10), BorderFactory.createEtchedBorder()));
panelForFilesPanelBorder.add(filesPanel);
buttonsPanel = new JPanel();
buttonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
buttonsPanel.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(5,10,10,10), BorderFactory.createEtchedBorder()));
buttonsPanel.add(startButton);
buttonsPanel.add(stopButton);
basePanel.add(panelForFilesPanelBorder);
basePanel.add(numericInputPanel);
basePanel.add(buttonsPanel);