简体   繁体   English

从Javafx中的另一个Java类访问一个Java类

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

I am unable to call a class from another class in my javafx application. 我无法从javafx应用程序中的另一个类调用一个类。 I make a pie-graph in java. 我用Java制作了一个饼图。 When called individually this runs successfully. 当单独调用时,它将成功运行。 But when I use this code through access from another class from this current class using this code 但是当我通过使用此代码从当前类中的另一个类访问时使用此代码

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

it is not working. 它不起作用。

And this is the graph code being called from the above, sitting in its own class. 这是上面的图形代码,位于其自己的类中。

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...) only works, if called from a class that extends application (and that class will be used as the Application 's class). 仅当从扩展应用程序的类中调用Application.launch(String...)时才起作用(该类将用作Application的类)。 If you want to start a javafx application from a different class, you need to use the Application.launch(Class, String... method: 如果要从其他类启动javafx应用程序,则需要使用Application.launch(Class, String...方法:

Application.launch(Graph.class);

Note that you must not start more than 1 Application from the same program. 请注意,从同一程序启动的Application不得超过1个。

(Move the code of the scene graph, ect. to different classes if you're planning to do this; eg using fxml+controller would be a good idea in that case) (如果打算这样做,请将场景图的代码移到不同的类;例如,在这种情况下,使用fxml + controller是个好主意)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM