首页 文章

Java和JFreeChart - 如何设置JFreeChart 's ChartPanel resize with it' s容器(比如JPanel)?

提问于
浏览
0

我刚刚发现当我将ChartPanel添加到JFrame时,ChartPanel将调整大小以适应框架,因为JFrame拖动更大或更小 . 但是,当我将ChartPanel添加到JPanel时,JPanel的大小(宽度和高度)会随着JFrame的大小调整而适应JFrame . 但ChartPanel只是保持它的尺寸,使放大的JPanel留下了很多空白空间 . 如何使ChartPanel在GUI中使用它的容器调整大小?

这是我的示例代码:

  • 这种情况下图表使用JFrame调整大小

ChartPanel chartPanel = new ChartPanel(createWhateverChart());
JFrame frame = new JFrame();
frame.add(chartPanel);

  • 在这种情况下,我首先将ChartPanel添加到JPanel,以便于管理和更新图表,但ChartPanel不会使用JPanel / JFrame调整大小 .

ChartPanel chartPanel = new ChartPanel(createWhateverChart());
JPanel面板=新JPanel(“Who cares”);
panel.add(chartPanel,BorderLayout.CENTER);
JFrame frame = new JFrame();
frame.add(面板);

1 回答

  • 3

    试试我的代码,它工作正常 .

    注意 :

    我有一个框架,其中包含panelMain,一个panelMain包含一个子面板,一个子面板包含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);
    

    现在,当您调整窗口应用程序的大小时 . 您的chartPanel将自动调整大小 . 希望这可以帮助 .

相关问题