0
自从上学期我参加了我的第一个编程课程后,我就成为了Java的初学者,我正在努力在今年夏天继续练习。我正在研究一些非常基本和简单的事情,但我发现我有一个问题。我们可以在JavaFX的另一个.setOnAction()块中写入.setOnAction()吗?
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("My little success story");
TextInputDialog entername = new TextInputDialog("Your name");
entername.setTitle("Player Information");
entername.setHeaderText("What is going to be the name of the `person we'll follow?");`
entername.setContentText("Please enter your name:");
Optional<String>result = entername.showAndWait();
if (result.isPresent()){
name = result.get();
}
Image roleplay1 =new Image("Roleplay - 1.jpg");
ImageView image1A = new ImageView();
image1A.setImage(roleplay1);
image1A.setFitHeight(400);
image1A.setFitWidth(400);
image1A.setSmooth(true);
root.getChildren().add(image1A);
Label welcome = new Label("Welcome "+name+" it's 2:00 `p.m."+newLine`
+"You either can go to your bio class or to the gym.");
HBox HWelcome = new HBox(welcome);
HWelcome.setAlignment(Pos.BOTTOM_CENTER);
HWelcome.setStyle("-fx-background-color: #faffff");
Button goToClass = new Button("Go to class");
goToClass.setPrefWidth(120.);
Button goToGym = new Button("Go to the gym");
goToGym.setPrefWidth(120.);
HBox choice = new HBox(goToClass, goToGym);
choice.setAlignment(Pos.BOTTOM_CENTER);
choice.setSpacing(70.);
choice.setPadding(new Insets(30));
root.setBottom(HWelcome);
root.setCenter(choice);
goToClass.setOnAction(new EventHandler <ActionEvent>(){
public void handle1(ActionEvent e){
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Roleplay");
Image roleplay2A = new Image("Roleplay - 2A.jpg");
ImageView image2A = new ImageView();
image2A.setImage(roleplay2A);
image2A.setSmooth(true);
image2A.setFitHeight(400.);
image2A.setFitWidth(400.);
root.getChildren().add(image2A);
Label scenario = new Label("You've arrived to class in `time."+newLine+`
"You are lucky, it was very important `today."+newLine+`
"You can team up with your best friend or with `the nerd.");`
HBox HinClass = new HBox(scenario);
HinClass.setAlignment(Pos.BOTTOM_CENTER);
HinClass.setStyle("-fx-background-color: #faffff");
Button yourFriend = new Button("Your friend");
yourFriend.setPrefWidth(120.);
Button theNerd = new Button("The nerd");
theNerd.setPrefWidth(120.);
HBox choice = new HBox(yourFriend, theNerd);
choice.setAlignment(Pos.BOTTOM_CENTER);
choice.setSpacing(70.);
choice.setPadding(new Insets(30));
root.setBottom(HinClass);
root.setCenter(choice);
}
yourFriend.setOnAction(new EventHandler <ActionEvent>(){
public void handle(ActionEvent e){
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Roleplay");
Image image = new Image("Roleplay 3 - `A.jpg");`
ImageView img = new ImageView(image);
root.getChildren().add(img);
img.setSmooth(true);
}
});
});
所以一切顺利,当我用我的第一个按钮,
goToClass.setOnAction(new EventHandler <ActionEvent>(){
但后来,当我尝试继续使用创建一个新的窗口相同的技术与每次两个按钮,用户点击在带有此代码的按钮上
它说有某种语法错误,我不能再继续。
我真的不知道我是否清楚,因为这是我发布的第一个问题,但我希望你们能帮助我!
非常感谢,