gpt4 book ai didi

java - GridBagLayout 中的 JScrollPane 影响应用程序的大小

转载 作者:行者123 更新时间:2023-11-30 03:36:21 25 4
gpt4 key购买 nike

我一直在练习 Java GUI 开发,在我的一个项目中我使用 GridBagLayout具有各种控件,例如 JLabel , JComboBoxe ,以及 JTable封装在 JScrollPane 中.

但是,我注意到 JScrollPane似乎影响了我的JPanel除非我的高度达到一定值(522 px 或更大,根据 .Pack() ),否则我的 JComboBoxe看起来“紧凑”,宽度将设置为适合文本而不是我指定的大小(200 px)。

所以我的问题是导致此问题的原因以及如何修复它,以便 JFrame的高度可以再小一点,不会影响我的JComboBoxe应该这样做。

调整大小之前/之后我的应用程序的外观的屏幕截图:

http://i59.tinypic.com/2i6ntc8.jpg

我的应用程序的当前代码:

package gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.SwingConstants;

public class MainWindow {

public static void main(String[] args) {
MainWindow main = new MainWindow();
JFrame frame = new JFrame();
JPanel panel = new JPanel();

JComboBox<String> cmbTeam, cmbPosition;
JLabel lblTeam, lblPosition, lblResults;
JTable table;
JSeparator sep1,sep2;

int rowCount;

GridBagConstraints gbc = new GridBagConstraints();

panel.setLayout(new GridBagLayout());
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = 1;

gbc.gridx = 0;
gbc.gridy = 0;
lblTeam = new JLabel("First Label");
panel.add(lblTeam, gbc);

gbc.gridx = 1;
lblPosition = new JLabel("Second Label");
panel.add(lblPosition, gbc);

gbc.gridx = 0;
gbc.gridy = 1;
cmbTeam = new JComboBox<>(new String[]{"Data1","Data2","Data3"});
cmbTeam.setPreferredSize(new Dimension(200,25));
panel.add(cmbTeam, gbc);

gbc.gridx = 1;
cmbPosition = new JComboBox<>(new String[]{"Data1","Data2","Data3"});
cmbPosition.setPreferredSize(new Dimension(200,25));
panel.add(cmbPosition, gbc);

gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
sep1 = new JSeparator(SwingConstants.HORIZONTAL);
panel.add(sep1,gbc);

gbc.gridy = 3;
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;
table = new JTable(new String[][]{{"AA","AB","AC"},{"BA","BB","BC"},{"CA","CB","CC"}}, new String[]{"Col1","Col2","Col3"});
table.setFillsViewportHeight(true);
panel.add(new JScrollPane(table),gbc);

gbc.gridy = 4;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
sep2 = new JSeparator(SwingConstants.HORIZONTAL);
panel.add(sep2,gbc);

rowCount = table.getRowCount();
lblResults = new JLabel(rowCount + " results found.");
gbc.gridy = 5;
panel.add(lblResults, gbc);

frame.setTitle("GridBagLayout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(panel);
frame.pack();
}

}

编辑:我通过为每个 JComboBox 设置最小尺寸来解决高度问题,但是由 .pack() 生成的空白区域仍然没有修复。我还注意到,如果我垂直调整窗口大小并使其变小,表格的宽度似乎会减少一个像素。也许这有什么关系?

最佳答案

what is causing this and

通常,当 GridbagLayout 中没有足够的空间以其首选大小显示组件时,该组件将捕捉到其最小大小。我不确定为什么更改高度会导致首选宽度发生变化,但不知何故,这导致组合框以其最小宽度显示。

how would I fix it so the JFrame's height can be smaller without causing my JComboBoxes to behave like that.

利用框架的默认BorderLayout。将组件单独添加到框架中:

//frame.add(panel);
frame.add(panel, BorderLayout.NORTH);
frame.add(new JScrollPane(table));
frame.add(lblResults, BorderLayout.SOUTH);

当然,您需要整理其余的代码,因为您不再向面板添加滚动 Pane 和标签。

当宽度减小时,您仍然会看到组合框缩小。

编辑:

but that whitespace that's generated from .pack() is still not fixed.

JTable 对“视口(viewport)”有一个硬编码的首选大小。您可以通过执行以下操作来覆盖它:

table.setPreferredScrollableViewportSize(table.getPreferredSize());

关于java - GridBagLayout 中的 JScrollPane 影响应用程序的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27802299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com