简体   繁体   中英

Call a class that extends another class from main method

So I have a class that draw a bar chart. I got the code from here:

https://docs.oracle.com/javafx/2/charts/bar-chart.htm

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class BarChartSample extends Application {
final static String austria = "Austria";
final static String brazil = "Brazil";
final static String france = "France";
final static String italy = "Italy";
final static String usa = "USA";

@Override public void start(Stage stage) {
    stage.setTitle("Bar Chart Sample");
    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);
    stage.setScene(scene);
    stage.show();
}

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

}

But the problem is I want to call this BarChartSample class from my main method in another class. For example, I have a class called BookRecommender and has a main method. I want to be able to set the parameters such as title, xlabel, ylabel and also values of this BarChartSample class. But I cannot run the start method of the BarChartSample class from my Recommender class.

This is a JavaFX application, so you wouldn't directly call this class from another class. You need to encapsulate the functionality of this class in a class and then call it from a JavaFX application.

Eg you could subclass Scene to create a specific BarChartScene and then pass the data you require in the constructor or a method setData([data you wish to pass]). Or you could create a BarChartSceneFactory class that would return a Scene object.

Eg SceneFactory.java

import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;

public abstract class SceneFactory {
    private static final String AUSTRIA = "Austria";
    private static final String BRAZIL = "Brazil";
    private static final String FRANCE = "France";
    private static final String ITALY = "Italy";
    private static final String USA = "USA";

    public static Scene getBarChartScene() {
        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 scene;
    }
}

MainApp.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainApp extends Application {
    @Override
    public void start(Stage stage) {
        Scene scene = SceneFactory.getBarChartScene();
        stage.setScene(scene);
        stage.setTitle("Bar Chart Sample");
        stage.show();
    }
}

You can then simple call SceneFactory.getBarChartScene() in your main sample application to get a Scene object and set it on your Stage.

If you wish your own custom data to be displayed, then pass it to the getScene method as a parameter eg Map<Integer, Map<String, Double>> chartData where the first integer holds the year, which maps to a mapping of country name to values.

Or you could subclass Scene and do it that way too eg

public class BarChartScene extends Scene {
    public BarChartScene([pass some data in here]) {
        // Build your scene based on the data passed in the constructor.
    }
}

Does your main class extend Application as well? If so, I would modify the code you have so that it doesn't extend Application. You can add methods to modify whatever data you need. You could even just copy and paste the code you need into your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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