【问题标题】:Open new BarChart Window (JavaFX) from inside a Swing GUI从 Swing GUI 中打开新的条形图窗口 (JavaFX)
【发布时间】:2013-09-24 16:03:59
【问题描述】:

当我单击按钮(在 swing jar 应用程序内)时,如何打开新的条形图窗口 (JavaFX)?

我已经有一个带有按钮处理程序的正常运行的 GUI,但我无法将 Oracle (http://docs.oracle.com/javafx/2/charts/bar-chart.htm) 中的示例与按钮连接起来,以便在我单击按钮时打开条形图。

我正在使用桌面 Java 开发工具包(版本 7 Update 40,64 位)和 Eclipse Juno。

基本上我试过这个:

...
BarChartSample chart = new BarChartSample();
Stage stage = new Stage();
chart.start(stage);
...

【问题讨论】:

    标签: java swing window javafx bar-chart


    【解决方案1】:

    最简单的方法是使用您的摆动窗口(包含图表的窗口)来完成。

    如果您这样做,代码将如下所示:

    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();
    

    上一行代码应该在a:

    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 类型的参数。我做错了什么?
    • 图表应该是类节点,而不是应用程序。那里小心。所以实际上你应该添加而不是图表到场景中的是“bc”,从那个例子。
    • 我编辑了原始答案,以便您可以看到应该添加的图表。
    猜你喜欢
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 2013-02-09
    • 2015-05-29
    • 2017-08-26
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多