【问题标题】:Java&JFreeChart - How to set a JFreeChart's ChartPanel resize with it's container ( say JPanel )?Java&JFreeChart - 如何设置 JFreeChart 的 ChartPanel 调整大小与它的容器(比如 JPanel)?
【发布时间】:2012-11-05 09:20:16
【问题描述】:

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

这是我的示例代码:

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

【问题讨论】:

  • 您为 JPanel 使用了哪个布局管理器?
  • 为了更好的帮助,请尽快发布SSCCE,up_to JPanel 拥有 JFreeChart,关于 contianers 的说明代码,没有 JFreeChart 的实现
  • re Heisenbug : 对于 JPanel 案例,BorderLayer。 ChartPanel 添加了 BorderLayout.CENTER
  • @Biscuitz:尝试改用 BoxLayout/FlowLayout,或者像建议的 mKorbel 那样发布 SSCCE。
  • re mKorbel:感谢您的建议。我只是认为我的代码在这个讨论中不是很有用。不管怎样,我把代码加回来了。

标签: java swing user-interface jfreechart


【解决方案1】:

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

请注意:

我有一帧包含一个 panelMain,一个 panelMain 包含一个 subPanel,一个 subPanel 包含 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 将自动调整大小。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 2011-08-13
    • 1970-01-01
    相关资源
    最近更新 更多