我是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>(){
它表示存在某种语法错误,我无法继续下去。
我真的不知道我是否已经清楚,因为这是我在这里发布的第一个问题,但我希望你们能够帮助我!
非常感谢,
答案 0 :(得分:0)
我没有测试过您的代码,但我只打算帮助您消除编译错误,因为您说您是Java初学者
package com.test;
import java.util.Optional;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextInputDialog;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Random
{
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();
String name = "";
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." + System.lineSeparator() +
"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>()
{
Button yourFriend = new Button("Your friend");
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 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." + System.lineSeparator() +
"You are lucky, it was very important `today." + System.lineSeparator() +
"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");
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);
}
});
}
});
}
finally
{
}
}
}