Java&JFreeChart-如何使用其容器(例如JPanel)设置JFreeChart的ChartPanel调整大小? [英] Java&JFreeChart - How to set a JFreeChart's ChartPanel resize with it's container ( say JPanel )?

查看:106
本文介绍了Java&JFreeChart-如何使用其容器(例如JPanel)设置JFreeChart的ChartPanel调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现,当我将ChartPanel添加到JFrame时,随着JFrame拖动的变大或变小,ChartPanel将调整大小以适合该框架.但是,当我将ChartPanel添加到JPanel时,随着JFrame调整大小,JPanel的大小(width和height)会更改以适合JFrame.但是,ChartPanel仅保留其尺寸,从而使扩大后的JPanel留有很多空白空间.如何使用GUI中的容器调整ChartPanel的大小?

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 ChartPanel to a JPanel, the JPanel's size( width and height ) changes to fit the JFrame as the JFrame resized. But the ChartPanel just keeps it's dimensions, leaving the enlarged JPanel a lot of empty space. How to make ChartPanel resize with it's container in GUI?

这是我的示例代码:

  1. 这种情况下,图表使用JFrame调整大小

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

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

ChartPanel chartPanel = new ChartPanel(createWhateverChart());
JPanel panel = new JPanel("Who cares");
panel.add(chartPanel, BorderLayout.CENTER);
JFrame frame = new JFrame();
frame.add(panel);

推荐答案

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

Try my code and it works fine.

请注意:

我有一帧包含一个panelMain,panelMain包含一个subPanel,一个subPanel包含ChartPanel.

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

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

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

这篇关于Java&JFreeChart-如何使用其容器(例如JPanel)设置JFreeChart的ChartPanel调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆