我有GridBagLayout,其中我添加了JLabel
,JTextfield
和两个JButtons
with和图像,图像是25x25像素。无论何时我显示或隐藏按钮,整个布局都会移动。我试图setSize()
,setMinimumSize()
,setPreferredSize()
和setMaximumSize()
但它会忽略那个调整大小的JLabel
和JTextField
。我怎样才能为这两个组件设置修复大小,或者让GridBagLayout中的所有单元格的大小相同。与JTextField和JButton的GridbagLayout,当JButton可见时重新格式化
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
panel.add(new JLabel("Auftrag Nummer:"), c);
JTextField orderNumberField = new JTextField();
c.gridx = 1;
c.gridy = 0;
panel.add(orderNumberField, c);
JButton searchButton = DefaultLayouts.imageButton("SEARCH");
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(10, 0, 0, 0);
panel.add(searchButton, c);
JButton checkButton = DefaultLayouts.imageButton("CHECK");
c.gridx = 1;
c.gridy = 1;
c.insets = new Insets(10, 0, 0, 0);
panel.add(checkButton, c);
DefautlLayouts.imageButton
获取路径和工具提示,并设置一些默认
public static JButton imageButton(String propertiesName) {
JButton button = DefaultLayouts.button();
button.setIcon(new ImageIcon(imagesProperties.getProperty(propertiesName + "_PATH")));
button.setToolTipText(imagesProperties.getProperty(propertiesName + "_TOOLTIP"));
button.setContentAreaFilled(false);
button.setFocusPainted(false);
button.setBorder(BorderFactory.createEmptyBorder());
return button;
}
不,这是非常期望的行为,因为不可见的组件不用于计算布局要求。也许使用空白图像或组件代替 – MadProgrammer
1)为了更快地获得更好的帮助,请发布[MCVE]或[简短,独立,正确的示例](http://www.sscce.org/)。 2)以最小尺寸提供ASCII艺术或简单的GUI图形*图形,并且如果可调整大小,则具有更大的宽度和高度。 (也可以应用该逻辑来确定组件是否因其可见性而被切换。) –