在普通的Java应用程序中是否可以使用JavaFX绘制图形? [英] Is it possible to draw graphs using JavaFX in a normal Java application?

查看:124
本文介绍了在普通的Java应用程序中是否可以使用JavaFX绘制图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想画一些在Java编码的模拟过程中收集到的统计图。我决定尝试JavaFX,因为它有一些很棒的图形功能。但是,由于我以前从未使用过它,因此我不确定是否可以将JavaFX功能添加到最初没有设置的项目中。



我已将javafx库添加到我的项目中,并将复制的JavaFX教程粘贴到线图上 http://docs.oracle.com/javafx/2/charts/line-chart.htm (没有主要功能)来测试图表是否正确显示。



然而,他们不这样做。我使用

  LineChartSample.launch(new String()函数将我的runsimulation函数(从SwingWorker线程中调用) ); 

并且在运行该程序并且没有看到任何图形后,我在通话后添加了一个println,并注意到该计划并没有达到它;即它在对LineChartSample的调用上终止。



我做错了什么?我试图甚至有可能吗?



编辑:这部分代码看起来像什么的简短摘要:



类InputGUI中的JButton调用

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
new AnswerWorker()。execute();
}


public class AnswerWorker扩展SwingWorker< Void,Integer> {

protected void doInBackground()抛出异常
{
AMEC.runsimulation();
返回null;
}
保护无效完成()
{
尝试{
JOptionPane.showMessageDialog(InputGUI.this,AMEC.unsuccesfulpercentage +迭代百分比有卡车必须睡过头);
AMEC.unsuccesfulpercentage = 0;

catch(Exception e)
{
e.printStackTrace();


$ b

并且AMEC.runsimulation调用 p>

  public static void runsimulation()throws IOException,InterruptedException {
...
LineChartSample.launch(new String ());
}

获取完成时AnswerWorker抛出的JDialogBox,但没有图形,每当我在LineChartSample.launch调用之后测试println时,它都不会到达。

解决方案

try

  public static void runsimulation()throws IOException,InterruptedException {
...
LineChartSample.launch(LineChartSample.class, );
}

获取更多信息:

http://docs.oracle.com/javafx/2/api/javafx/application/Application.html#launch%28java.lang.String...%29



请注意,您不应该这样做,因为您不能在应用程序生命周期中多次调用此代码。相反,您应该从构建场景图的LineChartSample中提取代码,并使用JFXPanel 将场景图嵌入到你的swing应用程序中。


I want to draw some graphs of statistics gathered during a simulation I coded in Java. I decided to try JavaFX, as it has some great graphing abilities. However, as I've never used it before, I'm not sure if it's even possible to add JavaFX capabilities to a project that wasn't set up to do this initially.

I've added the javafx library to my project, and copy pasted the JavaFX tutorial on line graphs at http://docs.oracle.com/javafx/2/charts/line-chart.htm (without the main function) to test if the graphs display properly.

They don't, however. I call the graph in my runsimulation function (which is called from a SwingWorker thread) with

LineChartSample.launch(new String());

and after running the program and not seeing any graph, I added a println after the call and noticed that the program doesn't ever reach it; i.e. it terminates on the call to LineChartSample.

Am I doing something wrong? Is what I'm trying even possible?

EDIT: A quick summary of what that part of the code looks like:

A JButton in class InputGUI calls

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
     new AnswerWorker().execute();
}


public class AnswerWorker extends SwingWorker<Void, Integer> {

protected Void doInBackground() throws Exception
{
    AMEC.runsimulation();
    return null;
}
protected void done()
{
try {
    JOptionPane.showMessageDialog(InputGUI.this, AMEC.unsuccesfulpercentage + "% of iterations had trucks that had to sleep over");
    AMEC.unsuccesfulpercentage = 0;
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

}

And AMEC.runsimulation calls

public static void runsimulation() throws IOException, InterruptedException {
...
    LineChartSample.launch(new String());
}

I get the JDialogBox that AnswerWorker throws when it's done, but no graph, and whenever I test for a println after my LineChartSample.launch call, it never gets reached.

解决方案

try

public static void runsimulation() throws IOException, InterruptedException {
    ...
    LineChartSample.launch(LineChartSample.class, "");
}    

for more information:

http://docs.oracle.com/javafx/2/api/javafx/application/Application.html#launch%28java.lang.String...%29

be aware that you shouldn't actually do it like this, because you can't call this code more than once in your applications lifetime. Instead, you should extract the code from the LineChartSample that builds the scene graph and use a JFXPanel to embed the scene graph into your swing application.

这篇关于在普通的Java应用程序中是否可以使用JavaFX绘制图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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