GridBagConstraints 填充垂直和水平

Dim*_*gin 1 java swing layout-manager gridbaglayout

如何填充感兴趣的网格单元中可用的垂直和水平空间?

例如:

JTextArea file1 = new JTextArea();
file1.setBorder(BorderFactory.createLineBorder(Color.black));
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.weightx = 1.0 / ELEMENTS_IN_WIDTH;
c.weighty = 0.9;
c.insets = new Insets(PAD, PAD, PAD, PAD);
c.fill = GridBagConstraints.VERTICAL;
mainPanel.add(file1, c);
Run Code Online (Sandbox Code Playgroud)

这会垂直填充单元格,但不会水平填充单元格。

c.fill = GridBagConstraints.HORIZONTAL显然在该值后面添加VERTICAL只会覆盖该值c.fill

OR喜欢它们c.fill = GridBagConstraints.HORIZONTAL | GridBagConstraints.VERTICAL也没有效果。

mos*_*tar 6

你应该使用:

GridBagConstraints gridConst = new GridBagConstraints();
gridConst.fill = GridBagConstraints.BOTH;
Run Code Online (Sandbox Code Playgroud)