从Swing GUI中打开一个新的JavaFX BarChart窗口

3
当我在单击按钮(在swing jar应用程序中)时,如何打开一个新的条形图窗口(JavaFX)?
我已经有了一个功能良好的GUI和一个按钮处理程序,但是我无法将Oracle的示例(http://docs.oracle.com/javafx/2/charts/bar-chart.htm)与按钮连接起来,以便在单击按钮时打开条形图。
我正在使用桌面Java开发工具包(版本7更新40,64位)和Eclipse Juno。
基本上,我尝试过这样做:
...
BarChartSample chart = new BarChartSample();
Stage stage = new Stage();
chart.start(stage);
...
1个回答

1

最简单的方法是使用Swing创建窗口(包含图表的窗口)。

如果这样做,代码看起来会像这样:

JFrame frame = new JFrame("Chart");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);

然后您应该将图表添加到fxPanel中,由于JavaFX是线程安全的,因此您将不得不使用Platform.runLater:

Platform.runLater(new Runnable() {
   @Override
   public void run() {
      BarChartSample chart = new BarChartSample();
      fxPanel.setScene(new Scene(chart));
   }
});

希望它有所帮助!
编辑:

图表应该长这样:

BarChart<String, Number> chart = getChart();

上一行代码应该在哪个位置:

Platform.runLater(new Runnable() {
       @Override
       public void run() {
         //CODE HERE
       }
    });

这是因为JavaFX是线程安全的。

创建它的方法如下:

public BarChart<String, Number> getChart() {
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final BarChart<String, Number> bc = new BarChart<String, Number>(xAxis,
                yAxis);
        bc.setTitle("Country Summary");
        xAxis.setLabel("Country");
        yAxis.setLabel("Value");

        XYChart.Series series1 = new XYChart.Series();
        series1.setName("2003");
        series1.getData().add(new XYChart.Data(austria, 25601.34));
        series1.getData().add(new XYChart.Data(brazil, 20148.82));
        series1.getData().add(new XYChart.Data(france, 10000));
        series1.getData().add(new XYChart.Data(italy, 35407.15));
        series1.getData().add(new XYChart.Data(usa, 12000));

        XYChart.Series series2 = new XYChart.Series();
        series2.setName("2004");
        series2.getData().add(new XYChart.Data(austria, 57401.85));
        series2.getData().add(new XYChart.Data(brazil, 41941.19));
        series2.getData().add(new XYChart.Data(france, 45263.37));
        series2.getData().add(new XYChart.Data(italy, 117320.16));
        series2.getData().add(new XYChart.Data(usa, 14845.27));

        XYChart.Series series3 = new XYChart.Series();
        series3.setName("2005");
        series3.getData().add(new XYChart.Data(austria, 45000.65));
        series3.getData().add(new XYChart.Data(brazil, 44835.76));
        series3.getData().add(new XYChart.Data(france, 18722.18));
        series3.getData().add(new XYChart.Data(italy, 17557.31));
        series3.getData().add(new XYChart.Data(usa, 92633.68));

        Scene scene = new Scene(bc, 800, 600);
        bc.getData().addAll(series1, series2, series3);
        return bc;
    }

谢谢。我从上面发布的链接中精确地复制了第一个示例,并将其放入 BarChartSample.java 中。但是你代码的问题在于 new Scene(chart) 对类型为 BarChartSample 的参数不适用。我做错了什么? - Kenneth Howlett
图表应该是节点类,而不是应用程序。请小心。 因此,您应该添加到场景中的是“bc”,就像那个例子一样。 - Magcus
我编辑了原始答案,这样你就可以看到应该添加什么图表。 - Magcus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接