gpt4 book ai didi

java - 调用 JTabbedPane 上的方法

转载 作者:太空宇宙 更新时间:2023-11-04 12:20:00 26 4
gpt4 key购买 nike

我使用 JFreeChart 和 SQL 创建了一个方法,该方法本质上是根据数据库中存在的数据创建一个图表。我有 3 种方法,其中一个具体的例子是用户体重与时间的关系。我希望在 JTabbedPane 中显示这些图表,但不确定如何显示。当我在一个选项卡上调用该方法时,它告诉我不允许使用静态/无效方法。我的方法没有参数。这是我的初步尝试:

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

编辑:在发现 a) 我无法调用组件的 void 方法和 B) 空布局无法缩放后,我回到了绘图板。这是我重写的方法,我希望调用 JTabbedPane,使该方法成为非 void。

 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 中的面板中,并使用单击监听器激活。 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)

最佳答案

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

关于java - 调用 JTabbedPane 上的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38942842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com