ChartPanel 不适合 JPanel 的大小 (JFreeChart)

ChartPanel not fiting the size of a JPanel (JFreeChart)

我只是在使用 ChartPanel 的方法 setSize。由于某种原因,它无法工作。这是代码:

    mostSoldPanel = new JPanel();
    chartTabbedPane.addTab("Mais vendidos", null, mostSoldPanel, null);
    mostSoldChart = ChartFactory.createBarChart("Mais vendidos", "Produtos", "Quantidade", createDataset(),
            PlotOrientation.VERTICAL, true, true, false);

    ChartPanel chartPanel = new ChartPanel(mostSoldChart);
    mostSoldPanel.add(chartPanel);
    chartPanel.setSize(mostSoldPanel.getSize());

这是视觉结果:

可能的原因是 JPanel 默认使用 FlowLayout,这允许子组件在布局时使用它们自己的 preferredSize

考虑将 mostSoldPanel = new JPanel(); 更改为 mostSoldPanel = new JPanel(new BorderLayout()); 并取消 get/setSize 调用