簡體   English   中英

從Javafx中的另一個Java類訪問一個Java類

[英]Access one Java class from another Java class in Javafx

我無法從javafx應用程序中的另一個類調用一個類。 我用Java制作了一個餅圖。 當單獨調用時,它將成功運行。 但是當我通過使用此代碼從當前類中的另一個類訪問時使用此代碼

Graph main = new Graph();
Application.launch();

它不起作用。

這是上面的圖形代碼,位於其自己的類中。

package semi_final1;
//package Graph;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.chart.*;
import javafx.scene.Group;
/**
 *
 * @author 
 */
public class Graph extends Application {
  @Override public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Analysis");
        stage.setWidth(500);
        stage.setHeight(500);
      ObservableList<PieChart.Data> pieChartData =
                FXCollections.observableArrayList(
                new PieChart.Data("Levels", 13),
                new PieChart.Data("Points", 25),
                new PieChart.Data("Questions", 10),
                new PieChart.Data("Answers", 22),
                new PieChart.Data("Length of Contents", 20),
                 new PieChart.Data("Likes", 20),
                new PieChart.Data("Dislikes", 10),
                 new PieChart.Data("Jaccard", 10));
        final PieChart chart = new PieChart(pieChartData);
        chart.setTitle("Imported Fruits");

        ((Group) scene.getRoot()).getChildren().add(chart);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        //launch(args);
    }
}

僅當從擴展應用程序的類中調用Application.launch(String...)時才起作用(該類將用作Application的類)。 如果要從其他類啟動javafx應用程序,則需要使用Application.launch(Class, String...方法:

Application.launch(Graph.class);

請注意,從同一程序啟動的Application不得超過1個。

(如果打算這樣做,請將場景圖的代碼移到不同的類;例如,在這種情況下,使用fxml + controller是個好主意)

暫無
暫無

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

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