簡體   English   中英

從Swing GUI中打開新的BarChart窗口(JavaFX)

[英]Open new BarChart Window (JavaFX) from inside a Swing GUI

當我點擊按鈕(在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);
...

最簡單的方法是用你的搖擺窗口(包含圖表的窗口)來做。

如果你這樣做,代碼看起來像這樣:

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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM