为什么items
组件没有附加到上一行的底部?
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField name = new JTextField("Name");
JButton submit = new JButton("Search");
JScrollPane items = new JScrollPane();
items.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
items.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.setLayout(new GridBagLayout());
frame.add(name, new GridBagConstraints (0, 0, 1, 1, 2, 1, GridBagConstraints.NORTHEAST, GridBagConstraints.HORIZONTAL,new Insets(5, 2, 2, 2),0,0));
frame.add(submit, new GridBagConstraints(-1, 0, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,new Insets(0, 0, 0, 0),0,0));
frame.add(items, new GridBagConstraints (0, -1, 2, 1, -1, 2, GridBagConstraints.NORTH, GridBagConstraints.BOTH,new Insets(0, 0, 0, 0),0,0));
frame.setPreferredSize(new Dimension(800, 600));
// frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
您将垂直权重(
weighty
,第六个参数GridBagConstraints
)设置为 1 和 2,因此第一行占据可用空间的三分之一。为第一行的组件指定 0。