2017-07-25 97 views
1

我只是使用ChartPanel的方法setSize。出於某種原因,它無法工作。下面的代碼:ChartPanel不符合JPanel的大小(JFreeChart)

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()); 

這裏的視覺效果:

enter image description here

+0

考慮更改'mostSoldPanel =新JPanel();'到'mostSoldPanel =新JPanel(新的BorderLayout());',擺脫了' get/setSize'調用 – MadProgrammer

回答

2

可能的原因是,JPanel默認使用一個FlowLayout,這是允許的子組件用自己的事實preferredSize當被佈置時。

考慮更改mostSoldPanel = new JPanel();mostSoldPanel = new JPanel(new BorderLayout());和擺脫get/setSize電話

+0

像魅力一樣工作。看來我的Java變得有點生疏了。謝謝! –