res*_*n84 5 java swing gridbaglayout
我正在为学校的项目制作GUI.我正在使用GridBagLayout.
我希望有一个标签指示输入(文件类型@ x = 0,y = 0),然后是另一个标签(实际文件名一旦选择@ x = 1, y = 0),然后是文件选择器的浏览按钮(@ x = 2, y = 0) .标签at (1,0)最初是空白的,但是当标签不包含文本时,我希望文本占据的区域占用一些空间.我还希望标签at (0,0)和按钮之间的空间(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);
Run Code Online (Sandbox Code Playgroud)
令人讨厌的是,没有文字的标签不会占用垂直空间." "当它为空时你可以使用它来解决这个问题.
JLabel fileNameLabel = new JLabel(" ");
Run Code Online (Sandbox Code Playgroud)
本质上,GridBagLayout 将组件放置在网格中的矩形(单元格)中,然后使用组件preferred sizes来确定单元格的大小。有了这个布局:
"")文本,如果没有显式使用或覆盖给它提供首选大小提示,则AJLabel\JTextFeild\JButton的首选大小为 around 。(2,2)setPreferredSize(size)getPreferredSize(size)在他看来,设置最小尺寸是行不通的,因为带有空文本的首选尺寸就在附近,(2,2)并且容器的尺寸已经大于首选尺寸。GridBagLayout使用首选尺寸,根本不用担心最小尺寸。
您实际上需要设置首选大小和最小大小,GridBagLayout因为:
如果您尝试仅设置 首选大小,则缩小窗口的大小最终会导致容器面板的大小缩小。容器的面板尺寸将小于组件的首选尺寸,并JLabel\JTextFeild\JButton 收缩到其最小尺寸。如果此时您还没有给出最小尺寸提示,则将(2,2)使用默认的最小尺寸:可能会在附近。通过下面给出的工作示例,您会更好地理解。
其他一些需要注意的事项:
c.gridx = 1;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
// filesPanel.add(calibrationFileSelection,c);
filesPanel.add(calibrationFileSelectionValuePanel,c);
Run Code Online (Sandbox Code Playgroud)
您正在添加panel包含您的calibrationFileSelectionValueLabel. 您不需要使用额外的面板,而只需calibrationFileSelectionValueLabel通过设置直接将其添加到网格(而不是覆盖getPreferedSize(Size)更好)preferredSize。不过,尝试将 inset 设置为 ,以GridBagConstraints使您的第一个看起来panel更好一点:
gridBagCnst.insets = new Insets(3, 3, 3, 3);
Run Code Online (Sandbox Code Playgroud)
给您一个最小的工作示例:
如果您不明白我们所说的,在这个例子中我仅设置了首选尺寸来解决您的问题。但由于我尚未设置最小尺寸,请尝试调整窗口大小以符合上述说明。

源代码:
import java.awt.*;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.border.*;
/**
*
* @author Rashed
*/
class GridBagLayoutDemo extends JFrame{
public JLabel createLabel(String txt, int width, int height, Color color)
{
JLabel label = new JLabel(txt);
label.setOpaque(true);
label.setBackground(color);
label.setPreferredSize(new Dimension(width, height));
return label;
}
public GridBagLayoutDemo() throws HeadlessException {
// setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new java.awt.GridBagLayout());
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
GridBagConstraints labCnst = new GridBagConstraints();
Dimension preferredSize = new Dimension(140,20);
labCnst.insets = new Insets(3, 3, 3, 3);
JLabel title = new JLabel("My Title");
JLabel title2 = new JLabel("My Title");
JLabel title3 = new JLabel("My Title");
JLabel selectionLabel1 = new JLabel("");
selectionLabel1.setBorder(new LineBorder(Color.BLACK));
JLabel selectionLabel2 = new JLabel("");
selectionLabel2.setBorder(new LineBorder(Color.BLACK));
JLabel selectionLabel3 = new JLabel("");
selectionLabel3.setBorder(new LineBorder(Color.BLACK));
selectionLabel1.setPreferredSize(preferredSize);
selectionLabel2.setPreferredSize(preferredSize);
selectionLabel3.setPreferredSize(preferredSize);
JButton browse1 = new JButton("Browse");
JButton browse2 = new JButton("Browse");
JButton browse3 = new JButton("Browse");
labCnst.gridx = 0;
labCnst.gridy = 0;
panel.add(title, labCnst);
labCnst.gridy = 1;
panel.add(title2, labCnst);
labCnst.gridy = 2;
panel.add(title3, labCnst);
labCnst.gridx = 1;
labCnst.gridy = 0;
panel.add(selectionLabel1, labCnst);
labCnst.gridy = 1;
panel.add(selectionLabel2, labCnst);
labCnst.gridy = 2;
panel.add(selectionLabel3, labCnst);
labCnst.gridx = 3;
labCnst.gridy = 0;
panel.add(browse1, labCnst);
labCnst.gridy = 1;
panel.add(browse2, labCnst);
labCnst.gridy = 2;
panel.add(browse3, labCnst);
//labCnst.anchor = GridBagConstraints.LINE_END;
add(panel);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GridBagLayoutDemo().setVisible(true);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11542 次 |
| 最近记录: |