【发布时间】:2017-07-06 04:58:58
【问题描述】:
自从上学期上第一堂编程课以来,我就是 Java 初学者,今年夏天我正努力继续练习。我正在做一些非常基本和简单的事情,但我发现我遇到了问题。
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>(){
但是,当我尝试继续使用相同的技术时,每次用户点击带有此代码的按钮时,使用两个按钮创建一个新窗口
yourFriend.setOnAction(new EventHandler <ActionEvent>(){
它说存在某种语法错误,我不能再进一步了。
我真的不知道我是否清楚,因为这是我在这里发布的第一个问题,但我希望你们能够帮助我!
非常感谢,
【问题讨论】: