简体   繁体   English

如何添加Onclick按钮以显示LineChart JavaFX

[英]How to add Onclick button to show the LineChart JavaFX

I was creating a student score system, the system connect to the database. 我正在创建一个学生成绩系统,该系统连接到数据库。 How can I write the code to show the JavaFX line chart when I click on the "Show Chart" button? 当我单击“显示图表”按钮时,如何编写代码以显示JavaFX折线图?

样本img

折线图img

This is the code of my line chart 这是我的折线图的代码

int i = 1;
@Override  
public void start(Stage primaryStage)   { 
    //define xAxis and yAxis
    NumberAxis xAxis=new NumberAxis(); // create xAxis
    xAxis.setLabel("Student"); // label xAxis

    NumberAxis yAxis=new NumberAxis(); // create yAxis
    yAxis.setLabel("Score"); // label yAxis

    //create line chart using xAxis and yAxis
    LineChart lineChart = new LineChart(xAxis, yAxis);

    //create data series
    XYChart.Series dataSeries1 = new XYChart.Series();
    dataSeries1.setName("Student Score Chart");

    try {  
        Class.forName("com.mysql.jdbc.Driver");  
        Connection con=(Connection) DriverManager.getConnection(DB_URL, USERNANE, PASSWORD);  

        Statement stmt=(Statement) con.createStatement();  
        ResultSet rs=(ResultSet) stmt.executeQuery("select * from student");  


        while(rs.next()){
        dataSeries1.getData().add(new XYChart.Data(i,(rs.getInt(1))));
        i++;
        }

        con.close();  
    } catch(Exception e) { 
            System.out.println(e);  
    } 

    //add data series to line chart

    lineChart.getData().add(dataSeries1);

    //add line chart to layout pane
    VBox vbox = new VBox(lineChart);
    //add layout pane to the scene
    Scene scene = new Scene(vbox, 400, 500);
    //add scene to the stage
    primaryStage.setScene(scene);
    primaryStage.show();
}

and the code of my button 和我按钮的代码

btnShowChart.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });

Here is a basic example ( mcve ): 这是一个基本示例( mcve ):

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class FxMain extends Application {

    @Override
    public void start(Stage primaryStage)   {

        //define xAxis and yAxis
        NumberAxis xAxis=new NumberAxis(); // create xAxis
        xAxis.setLabel("Student"); // label xAxis

        NumberAxis yAxis=new NumberAxis(); // create yAxis
        yAxis.setLabel("Score"); // label yAxis

        //create line chart using xAxis and yAxis
        LineChart lineChart = new LineChart(xAxis, yAxis);

        //create data series
        XYChart.Series dataSeries1 = new XYChart.Series();
        dataSeries1.setName("Student Score Chart");

        for(int i = 0; i <=10 ; i++){
             dataSeries1.getData().add(new XYChart.Data(i, i*3));
        }

        //add data series to line chart
        lineChart.getData().add(dataSeries1);

        //add line chart to layout pane
        VBox vbox = new VBox();
        Button btnShowChart = new Button("Show");
        btnShowChart.setOnAction(e-> {
            vbox.getChildren().add(lineChart);
            btnShowChart.setDisable(true);
        });
        BorderPane root = new BorderPane(vbox);
        root.setBottom(btnShowChart);
        //add layout pane to the scene
        Scene scene = new Scene(root, 400, 500);
        //add scene to the stage
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

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

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