校对请求:没有带红色下划线的代码,但是如果我尝试显示LineChart,则会收到错误消息。有人可以检查一下吗?

问题描述 投票:0回答:1
package VideoStuff;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author 38323
 */
public class ClassStuf extends Application {

    Stage window;
    GridPane grid;
    int x1, x2, x3, y1, y2, y3;
    TextField answer1, xAnswer, yAnswer;
    String graphtitle, yaxis, xaxis, xcoordinate1, xcoordinate2, xcoordinate3, ycoordinate1, ycoordinate2, ycoordinate3;
    Scene scene1, scene2, scene3, scene4;
    VBox layout;

    @Override
    public void start(Stage primaryStage) {

        window = primaryStage;
        grid = new GridPane();
        grid.setAlignment(Pos.TOP_LEFT);
        grid.setPadding(new Insets(10));
        grid.setVgap(10);
        grid.setHgap(10);
        Label graphquestion = new Label("Title of graph?");
        GridPane.setConstraints(graphquestion, 0, 0);
        answer1 = new TextField();
        GridPane.setConstraints(answer1, 0, 1);
        Button button = new Button("Sumbit");
        GridPane.setConstraints(button, 0, 2);
        button.setOnAction(e -> {
            graphtitle = answer1.getText();
        });
        grid.getChildren().addAll(graphquestion, answer1, button);
        scene1 = new Scene(grid, 300, 200);
        window.setScene(scene1);

        //Scene 2 (X and Y axis)


        layout = new VBox(15);
        scene2 = new Scene(layout, 400, 200);
        button.setOnAction(e -> window.setScene(scene2));
        Label xAxis = new Label("X Axis?");
        xAnswer = new TextField();
        Label yAxis = new Label("Y Axis?");
        yAnswer = new TextField();
        Button nextbutton = new Button("Proceed");
        nextbutton.setOnAction(e -> {
             yaxis = yAnswer.getText();
             xaxis = xAnswer.getText();
        });
        layout.getChildren().addAll(xAxis, xAnswer, yAxis, yAnswer, nextbutton);

        //X and Y inputs


        VBox box2 = new VBox(30);
         scene3 = new Scene(box2, 800, 800);
        nextbutton.setOnAction(e -> window.setScene(scene3));
        Label xvalue1 = new Label("1st x value?");
        TextField xanswer1 = new TextField();
        Label xvalue2 = new Label("2st x value?");
        TextField xanswer2 = new TextField();
        Label xvalue3 = new Label("3st x value?");
        TextField xanswer3 = new TextField();
        Button xclick = new Button("Submit x values");
        Label yvalue1 = new Label("1st y value?");
        TextField yanswer1 = new TextField();
        Label yvalue2 = new Label("2st y value?");
        TextField yanswer2 = new TextField();
        Label yvalue3 = new Label("3st y value?");
        TextField yanswer3 = new TextField();
        Button yclick = new Button("Submit y values");
        xclick.setOnAction(e
                -> {
             xcoordinate1 = xanswer1.getText();
             xcoordinate2 = xanswer2.getText();
             xcoordinate3 = xanswer3.getText();
            x1 = Integer.parseInt(xcoordinate1);
            x2 = Integer.parseInt(xcoordinate2);
            x3 = Integer.parseInt(xcoordinate3);

        });
        yclick.setOnAction(e
                -> {
             ycoordinate1 = yanswer1.getText();
             ycoordinate2 = yanswer2.getText();
             ycoordinate3 = yanswer3.getText();
            y1 = Integer.parseInt(ycoordinate1);
            y2 = Integer.parseInt(ycoordinate2);
            y3 = Integer.parseInt(ycoordinate3);
        });
        box2.getChildren().addAll(xvalue1, xanswer1, xvalue2, xanswer2, xvalue3, xanswer3, xclick,
                yvalue1, yanswer1, yvalue2, yanswer2, yvalue3, yanswer3, yclick);
        System.out.println(xaxis);
//LineChart Creation 
HBox root = new HBox();
            CategoryAxis xline = new CategoryAxis();
        xline.setLabel(xaxis); 
        NumberAxis yline = new NumberAxis();
        yline.setLabel(yaxis);
        LineChart<String, Number> linechart = new LineChart<String, Number>(xline, yline);
        linechart.setTitle(graphtitle);
        XYChart.Series<String, Number> data = new XYChart.Series<>();
        data.setName(xaxis);
        data.getData().add(new XYChart.Data<>((xcoordinate1), (y1)));
        data.getData().add(new XYChart.Data<>((xcoordinate2), (y2)));
        data.getData().add(new XYChart.Data<>((xcoordinate3), (y3)));
         linechart.getData().add(data);
         root.getChildren().add(linechart);
         scene4 = new Scene(root, 350, 330);
         yclick.setOnAction(e -> window.setScene(scene4));
         window.setTitle("LineChart Demo");

        window.show();

    }

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

}

就像一个提示,代码运行得很好,直到双斜杠表示LineChart Creation。我为自己的凌乱代码表示歉意,因为我仍然不熟悉JavaFX。尽管没有红线,但我仍在努力确定折线图为何不起作用的问题。应该发生的是,用户应该能够输入所有值,这些值取决于它们创建的x / y坐标值等。更不用说,它必须在GUI而不是纯文本上进行演示。

javafx netbeans linechart
1个回答
0
投票

启动应用程序时,仅执行事件处理程序之外的start方法中的代码。如果用户以某种方式与GUI交互,则事件处理程序内的任何代码都可以在start方法返回后稍后执行。

这意味着用户的任何数据输入都会在您填充数据之后发生。

结果是您在填充数据时有效地执行了以下逻辑:

data.getData().add(new XYChart.Data<>((String) null, 0));
data.getData().add(new XYChart.Data<>((String) null, 0));
data.getData().add(new XYChart.Data<>((String) null, 0));

您需要延迟填充数据,直到执行显示数据的事件处理程序为止。

此外,在代码的以下部分中,您将完全替换第一个事件处理程序。第一个事件处理程序对结果没有任何影响。

yclick.setOnAction(e
        -> {
     ycoordinate1 = yanswer1.getText();
     ycoordinate2 = yanswer2.getText();
     ycoordinate3 = yanswer3.getText();
    y1 = Integer.parseInt(ycoordinate1);
    y2 = Integer.parseInt(ycoordinate2);
    y3 = Integer.parseInt(ycoordinate3);
});
...
yclick.setOnAction(e -> window.setScene(scene4));

这不是您的代码中唯一出现此问题的部分。您对button(第一个场景)和nextbutton在第二个场景中执行相同的操作。只需将两个事件处理程序中的逻辑合并到一个事件处理程序中即可。

注:一件看起来很奇怪的事情是,即使您的代码正常运行,也需要2个用户交互才能显示数据。通常,您尝试最小化用户必须进行的必要交互。我希望单击一个按钮就可以分析x和y坐标的数据并显示数据。

/**
 * Helper method to avoid repetition
 */
private static void addPoint(XYChart.Series<String, Number> series, TextField xText, TextField yText) {
    int y = Integer.parseInt(yText.getText());
    series.getData().add(new XYChart.Data<>(xText.getText(), y));
}

...

someButton.setOnAction(evt -> {
    CategoryAxis xline = new CategoryAxis();
    xline.setLabel(xaxis); 
    NumberAxis yline = new NumberAxis();
    yline.setLabel(yaxis);
    LineChart<String, Number> linechart = new LineChart<String, Number>(xline, yline);
    linechart.setTitle(graphtitle);
    XYChart.Series<String, Number> data = new XYChart.Series<>();
    data.setName(xaxis);

    // data is read here
    addPoint(data, xanswer1, yanswer1);
    addPoint(data, xanswer2, yanswer2);
    addPoint(data, xanswer3, yanswer3);

    linechart.getData().add(data);
    root.getChildren().add(linechart);
    Scene scene4 = new Scene(root, 350, 330);

    // logic from the second event handler needs to be merged into a single event handler
    window.setScene(scene4);
});
© www.soinside.com 2019 - 2024. All rights reserved.