在最大化屏幕上调整JFreeChart.ChartPanel的大小

时间:2016-08-11 13:56:09

标签: java swing jfreechart

我有一个JFrame SELECT DISTINCT loc.id as [Node ID] , m.name as [Node Name] , isp.name as [ISP] , geo.name as [City] , CASE WHEN loc.object_type_id = 17 THEN 'Client' WHEN loc.synthetic_location.object_type_id = 5 THEN 'System' WHEN loc.object_type_id IS NULL THEN 'System' END AS Type FROM [db1].[synthetic_location] loc JOIN [db2].[machine] m ON (m.synthetic_location_id IS NULL and m.status_type_id < 1) JOIN [db3].[internet_service_provider] isp ON (isp.id = loc.internet_service_provider_id) JOIN [db4].[geography_city] geo ON (geo.id = loc.geography_city_id) WHERE loc.flags = 6 ,其中包含frame面板。我还在面板中添加了JPanel。到目前为止我所做的是在我的框架JFreeChart ChartPanel cp上使用ComponentListener来调整窗口大小。

这是我的代码段:

frame

更改帧的大小时,private void initialize() throws ParseException { frame = new JFrame(); frame.addComponentListener(new ComponentListener() { @Override public void componentResized(ComponentEvent e) { ChartPanel cp = (ChartPanel)panel.getComponents()[0]; cp.setPreferredSize(new java.awt.Dimension(panel.getWidth(), panel.getHeight())); frame.repaint(); cp.repaint(); } @Override public void componentMoved(ComponentEvent e) {} @Override public void componentShown(ComponentEvent e) {} @Override public void componentHidden(ComponentEvent e) {} }); ... } 会完美调整大小。但是当我最大化框架时,图表面板不会改变它的大小!它与最大化之前保持一致。因此,当我再次“取消最大化”我的框架时,图表面板会最大化,这会导致问题,图表将不再适合图表面板。所有这些调整大小的事情似乎都“落后了一步......”

有没有人知道这种行为并帮助如何纠正它?

1 个答案:

答案 0 :(得分:1)

ChartPanel.setSizeChartPanel.setPreferredSize完成工作后致电JFrame.validate!请参阅新代码:

frame = new JFrame();
frame.addComponentListener(new ComponentListener() {
    @Override
    public void componentResized(ComponentEvent e) {
        // frame.repaint();
        ChartPanel cp = (ChartPanel)panel.getComponents()[0];
        cp.setPreferredSize(new java.awt.Dimension(panel.getWidth(), panel.getHeight()));
        cp.setSize(new java.awt.Dimension(panel.getWidth(), panel.getHeight()));
        // frame.invalidate();
        frame.validate();
        // frame.repaint();
    }
    ...
});
...