我在GridBagLayout-GUI中遇到垂直填充问题。这是代码:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Spielklasse {
public static void main(String[] args) {
JPanel page = new JPanel();
page.setLayout(new GridBagLayout());
for (int i = 0; i < 5; i++) {
JPanel subPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel();
label.setText("label " + i + ":");
JTextField textField = new JTextField();
textField.setPreferredSize(new Dimension(120,20));
subPanel.add(label, BorderLayout.WEST);
subPanel.add(textField, BorderLayout.EAST);
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTHWEST;
c.gridx = 1;
c.gridy = i;
c.weightx = 1;
c.weighty = 1;
c.insets = new Insets(10, 20, 10, 20);
c.fill = GridBagConstraints.HORIZONTAL;
page.add(subPanel, c);
}
JScrollPane scrollPane = new JScrollPane(page);
JFrame frame = new JFrame();
frame.setSize(300,500);
frame.add(scrollPane);
frame.setVisible(true);
}
}
我希望标签+文本字段直接位于前一个之间,而不填充它们之间的垂直空间(就像垂直调整我的示例直到你可以看到垂直滚动条一样)。我怎样才能做到这一点?使用我的代码,整个面板垂直填充,但我不希望这样。
答案 0 :(得分:4)
删除下一行c.weighty = 1;
。
在docs中阅读更多内容。
编辑:使用虚拟JLabel
获取可用空间,循环后:
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 5;
c.fill = GridBagConstraints.BOTH;
c.weightx=1;
c.weighty=1;
c.gridwidth = 2;
page.add(new JLabel(" "),c);
答案 1 :(得分:1)
我对您的代码进行了一些更改。
始终通过调用SwingUtilities invokeLater方法启动Swing应用程序。这会将Swing组件放在Event Dispatch thread。
由于您使用的是GridBagLayout,因此您可以直接在JPanel页面上布置标签和文本字段。
我为每个标签和文本字段创建了一个GridBagConstraints。这样,我们不依赖于默认值。我们为每个组件指定正确的锚点和填充值。
我添加了几个缺少的JFrame方法。您必须指定默认关闭操作,或者您必须自己关闭应用程序。如果没有此规范,您的应用程序将在关闭主JFrame后继续运行。
编辑添加:
这是修改后的代码。
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Spielklasse implements Runnable {
private static final Insets baseInsets =
new Insets(10, 20, 10, 20);
@Override
public void run() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new FlowLayout());
JPanel page = new JPanel();
page.setLayout(new GridBagLayout());
int gridy = 0;
for (int i = 0; i < 5; i++) {
JLabel label = new JLabel();
label.setText("Label " + i + ":");
addComponent(page, label, 0, gridy,
1, 1, baseInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JTextField textField = new JTextField(20);
addComponent(page, textField, 1, gridy++,
1, 1, baseInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
}
mainPanel.add(page);
JScrollPane scrollPane = new JScrollPane(mainPanel);
JFrame frame = new JFrame("Spielklasse");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane);
frame.pack();
frame.setVisible(true);
}
private void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight,
Insets insets, int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0D, 1.0D, anchor,
fill, insets, 0, 0);
container.add(component, gbc);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Spielklasse());
}
}