gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:40:24 26 4
gpt4 key购买 nike

我正在创建一个学生评分系统,该系统连接到数据库。如何编写代码以在单击“显示图表”按钮时显示 JavaFX 折线图?

Sample img

line chart img

这是我的折线图的代码

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();
}

和我的按钮的代码

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

}
});

最佳答案

这是一个基本示例 (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);
}
}

关于java - 如何添加 Onclick 按钮以显示 LineChart JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55548806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com