校对请求:没有红色下划线的代码,但如果我试图显示折线图,我会收到一个错误.有人能检查一下吗


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上演示,而不是纯文本。

启动应用程序时,只执行事件处理程序之外的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执行相同的操作。只需将两个事件处理程序的逻辑合并为一个事件处理。

注意:有一件事看起来很奇怪,那就是即使代码正常工作,也需要两个用户交互来显示数据。通常你会尽量减少用户必须进行的必要交互。我希望点击一个按钮来解析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);
});

相关内容

最新更新