Java&JFreeChart - How to set a JFreeChart's ChartPanel resize with it's container ( say JPanel )?

后端 未结 1 1974
长情又很酷
长情又很酷 2021-01-16 19:27

I just found that when i add a ChartPanel to a JFrame, the ChartPanel will resize to fit the frame as the JFrame dragged larger or smaller. However, when i add the ChartPane

1条回答
  •  借酒劲吻你
    2021-01-16 19:52

    Try my code and it works fine.

    Note that :

    I have one frame which contains a panelMain, A panelMain contains a subPanel and a subPanel contains ChartPanel.

    frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));                      
    JPanel panelMain = new JPanel(new GridLayout(0,2));            
    ChartPanel chartPanel = createChart();        
    JPanel subPanel = new JPanel(new BorderLayout());   
    subPanel.setBorder(BorderFactory.createTitledBorder("Consommation"));
    subPanel.setPreferredSize(new Dimension(400, 200));    
    subPanel.add(chartPanel);   
    
    
    panelMain.add(subPanel);        
    frame.add(panelMain);        
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    

    And now when you resize your window application. Your chartPanel will resized automatically. Hope this helps.

    0 讨论(0)
提交回复
热议问题