我正面临着JPanel如何根据其拥有的组件数量自行调整大小的问题。这里总结了上下文:
第一种情况下,包含条形图的面板大小 310 ,而在第二种情况下, 315 。这个,当两个案例中唯一改变的是栏数时,它们作为单独的列添加到内容面板中。
有人能指点我如何调试这个问题吗?我应该在哪里看?在我看来,小组所采取的额外垂直空间是没有根据的,但我没理由。
代码的相关部分如下:
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0};
gridBagLayout.rowHeights = new int[]{0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 1.0};
gridBagLayout.rowWeights = new double[]{1.0};
setLayout(gridBagLayout);
YAxis yAxis = new YAxis(start, end);
GridBagConstraints gbc_yAxis = new GridBagConstraints();
gbc_yAxis.gridx = 0;
gbc_yAxis.gridy = 0;
gbc_yAxis.fill = GridBagConstraints.BOTH;
add(yAxis, gbc_yAxis);
JPanel chartsContainer = new JPanel() {
public void paint(Graphics g) {
super.paint(g);
// System.out.println(getSize());
}
};
chartsContainer.setOpaque(false);
GridBagLayout gbl_chartsContainer = new GridBagLayout();
int[] colWidths = new int[statsToDisplay.size() + 1];
Arrays.fill(colWidths, 25);
colWidths[colWidths.length-1] = 0;
gbl_chartsContainer.columnWidths = colWidths;
double[] colWeights = new double[statsToDisplay.size()+1];
Arrays.fill(colWeights, 0);
colWeights[colWeights.length-1] = 1.0;
gbl_chartsContainer.rowHeights = new int[] {50};
gbl_chartsContainer.columnWeights = colWeights;
gbl_chartsContainer.rowWeights = new double[]{1.0};
chartsContainer.setLayout(gbl_chartsContainer);
/* Code to fill chartsContainer goes here */
JScrollPane chartScrollPane = new JScrollPane(chartsContainer);
chartScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
chartScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
GridBagConstraints gbc_chartScrollPane = new GridBagConstraints();
gbc_chartScrollPane.fill = GridBagConstraints.BOTH;
gbc_chartScrollPane.gridx = 1;
gbc_chartScrollPane.gridy = 0;
add(chartScrollPane, gbc_chartScrollPane);