将代码转换为函数并链接问题。

huangapple go评论75阅读模式
英文:

Converting the code into a function and link problem

问题

这段代码展示了一个图表。您想将这段代码分割成一个函数,以便在调用该函数时执行代码并显示图形。不过在代码中有一些关于主要部分、舞台(stage)、场景(scene)等方面的链接,可能不太容易进行分割。如果您尝试过但仍然遇到问题,可以提供更多具体的信息,以便我能够更好地帮助您。感谢您的理解。

英文:

I have a problem maybe it is simple .. I hope for your help

This code below shows a graph.

public class LineChartSample extends Application {
 
    @Override public void start(Stage stage) {
        stage.setTitle("Line Chart Sample");
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
         xAxis.setLabel("Month");
        final LineChart<String,Number> lineChart = 
                new LineChart<String,Number>(xAxis,yAxis);
       
        lineChart.setTitle("Stock Monitoring, 2010");
                          
        XYChart.Series series1 = new XYChart.Series();
        series1.setName("Portfolio 1");
        
        series1.getData().add(new XYChart.Data("Jan", 23));
        series1.getData().add(new XYChart.Data("Feb", 14));
        series1.getData().add(new XYChart.Data("Mar", 15));
        series1.getData().add(new XYChart.Data("Apr", 24));
        series1.getData().add(new XYChart.Data("May", 34));
        series1.getData().add(new XYChart.Data("Jun", 36));
        series1.getData().add(new XYChart.Data("Jul", 22));
        series1.getData().add(new XYChart.Data("Aug", 45));
        series1.getData().add(new XYChart.Data("Sep", 43));
        series1.getData().add(new XYChart.Data("Oct", 17));
        series1.getData().add(new XYChart.Data("Nov", 29));
        series1.getData().add(new XYChart.Data("Dec", 25));
        
        XYChart.Series series2 = new XYChart.Series();
        series2.setName("Portfolio 2");
        series2.getData().add(new XYChart.Data("Jan", 33));
        series2.getData().add(new XYChart.Data("Feb", 34));
        series2.getData().add(new XYChart.Data("Mar", 25));
        series2.getData().add(new XYChart.Data("Apr", 44));
        series2.getData().add(new XYChart.Data("May", 39));
        series2.getData().add(new XYChart.Data("Jun", 16));
        series2.getData().add(new XYChart.Data("Jul", 55));
        series2.getData().add(new XYChart.Data("Aug", 54));
        series2.getData().add(new XYChart.Data("Sep", 48));
        series2.getData().add(new XYChart.Data("Oct", 27));
        series2.getData().add(new XYChart.Data("Nov", 37));
        series2.getData().add(new XYChart.Data("Dec", 29));
        
        XYChart.Series series3 = new XYChart.Series();
        series3.setName("Portfolio 3");
        series3.getData().add(new XYChart.Data("Jan", 44));
        series3.getData().add(new XYChart.Data("Feb", 35));
        series3.getData().add(new XYChart.Data("Mar", 36));
        series3.getData().add(new XYChart.Data("Apr", 33));
        series3.getData().add(new XYChart.Data("May", 31));
        series3.getData().add(new XYChart.Data("Jun", 26));
        series3.getData().add(new XYChart.Data("Jul", 22));
        series3.getData().add(new XYChart.Data("Aug", 25));
        series3.getData().add(new XYChart.Data("Sep", 43));
        series3.getData().add(new XYChart.Data("Oct", 44));
        series3.getData().add(new XYChart.Data("Nov", 45));
        series3.getData().add(new XYChart.Data("Dec", 44));
        
        Scene scene  = new Scene(lineChart,800,600);       
        lineChart.getData().addAll(series1, series2, series3);
       
        stage.setScene(scene);
        stage.show();
    }
 
 
    public static void main(String[] args) {
        launch(args);
    }
}

But I want to divide this code into a function .. when I call it, it executes the code and the graphic appears

I tried many times, but there are links in the code and I do not know how to collect them, especially in the main part and the stage and scene part

I hope for help
Thanks

答案1

得分: 1

依我看,您可以从您的代码中提取三个函数。

首先是 XYChart.Series getSeries(seriesName),它返回一个填充了数据的 Series 对象。

其次是 getScene(lineCharts, width, height),它返回一个添加了系列的 Scene 对象。

第三是 showScene(stage),在这个方法中,您可以使用上述方法设置您的舞台(stage),然后调用 stage.show() 来显示舞台。

英文:

In my opinion, you can extract three function from you code.

First is XYChart.Series getSeries(seriesName) which return a Series object with filled data.

Second is getScene(lineCharts,width,height) which return a Scene object with added series.

Third is showScene(stage), in this method you setup your stage with the above method and call stage.show() to show the stage.

huangapple
  • 本文由 发表于 2020年9月18日 04:33:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63945831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定