在JTabbedPane

时间:2016-08-14 13:46:05

标签: java jfreechart

我使用JFreeChart和SQL创建了一个方法,它基本上根据数据库中的数据创建了一个图形。我有3种方法,一个特定的例子,用户权重与时间。我想在JTabbedPane中显示这些图表,我不确定如何。当我在一个选项卡上调用该方法时,它告诉我不允许使用static / void方法。我的方法没有参数。这是我最初的尝试:

    panelWeight = new JPanel();
    panelWeight.setLayout(null);
    panelWeight.add(StatGraph.WeightGraph());

修改: 在发现a)之后我回到了绘图板,我无法对组件调用void方法,B)null布局不能缩放。这是我重写的方法,我希望调用JTabbedPane,使该方法无效。

 public static ChartFrame WeightGraph(){

    ChartFrame returnFrame = null;

     try{
        ConnectionManager connectionManager = ConnectionManager.getInstance();
        Connection connection = connectionManager.getConnection();
        UserInfoManager user = new UserInfoManager();
        int username = user.getId();

        String query = "SELECT DATE, WEIGHT FROM STATS WHERE ID=" + username;            JDBCCategoryDataset dataset = new JDBCCategoryDataset(connection, query);
        JFreeChart chart = ChartFactory.createLineChart("WEIGHTvsDATE Chart", "Date", "Weight", dataset, PlotOrientation.VERTICAL, false, true, true);
        BarRenderer renderer = null;
        CategoryPlot plot = null;
        renderer = new BarRenderer();
        ChartFrame frame = new ChartFrame("Progress Log", chart);
        returnFrame = frame;
        //frame.setVisible(true);
        //frame.setSize(750,400);
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null,e);

    }

     return returnFrame;
}

然后我返回GUI框架并将以下代码添加到TabbedPane中的Panel,使用单击侦听器激活。           panelWeight.add(StatGraph.WeightGraph()); 但是,我收到以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Container.java:488)
at java.awt.Container.addImpl(Container.java:1089)
at java.awt.Container.add(Container.java:415)
at GUI.PT.ProgressStatsPT.panelWeightMouseClicked(ProgressStatsPT.java:392)
at GUI.PT.ProgressStatsPT.access$600(ProgressStatsPT.java:26)
at GUI.PT.ProgressStatsPT$7.mouseClicked(ProgressStatsPT.java:138)
at java.awt.Component.processMouseEvent(Component.java:6528)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:0)

Graph.weightLg()返回的是什么? JPanel 中的add()方法采用 Component 或子类的实例。如果 Graph.weightLg()方法没有返回任何内容(我认为是你的情况),编译将失败。