我正在编写一个测验应用程序,该应用程序将包含多个不同的类,包括每个问题的单独类。我想使用Alert Boxes为用户提供进入下一个问题的选项,所以当我按下继续进入第一个问题时,它不会改变到下一个场景,我不太明白buttonType是如何工作的? 任何帮助将不胜感激
package pkg1;
import java.io.*;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Alert.*;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.Window;
public class Register {
Stage stage;
public Register (Stage b){
stage = b;
}
public void start(Stage stage){
stage.setTitle("Registration");
GridPane gridPane = createRegisterPane();
addUIControls(gridPane);
Scene scene = new Scene(gridPane, 800, 500);
stage.setScene(scene);
stage.show();
}
private GridPane createRegisterPane() {
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
gridPane.setPadding(new Insets(40, 40, 40, 40));
gridPane.setHgap(10);
gridPane.setVgap(10);
return gridPane;
}
private void addUIControls(GridPane gridPane) {
// Add Header
Label headerLabel = new Label("Registration");
headerLabel.setFont(Font.font("Arial", FontWeight.BOLD, 24));
gridPane.add(headerLabel, 0,0,2,1);
GridPane.setHalignment(headerLabel, HPos.CENTER);
GridPane.setMargin(headerLabel, new Insets(20, 0,20,0));
// FULL NAME
Label fnamelabel = new Label("Full Name : ");
gridPane.add(fnamelabel, 0,1);
// FULL NAME TEXT
TextField fnamefield = new TextField();
fnamefield.setPrefHeight(40);
gridPane.add(fnamefield, 1,1);
fnamefield.setPromptText(" - Enter your full name - ");
// EMAIL
Label emaillabel = new Label("Email: ");
gridPane.add(emaillabel, 0, 2);
// EMAIL TEXT
TextField emailfield = new TextField();
emailfield.setPrefHeight(40);
gridPane.add(emailfield, 1, 2);
emailfield.setPromptText(" - Enter your Email - ");
// USERNAME
Label usernamelabel = new Label("Username: ");
gridPane.add(usernamelabel, 0, 3);
// USERNAME TEXT
TextField usernamefield = new TextField();
usernamefield.setPrefHeight(40);
gridPane.add(usernamefield, 1, 3);
usernamefield.setPromptText(" - Enter your username - ");
// PASSWORD
Label passwordlabel = new Label("Password: ");
gridPane.add(passwordlabel, 0, 4);
// PASSWORD TEXT
PasswordField passwordfield = new PasswordField();
passwordfield.setPrefHeight(40);
gridPane.add(passwordfield, 1, 4);
passwordfield.setPromptText(" - Enter your password - ");
// SUBMIT BUTTON
Button submitButton = new Button("Submit");
submitButton.setPrefHeight(40);
submitButton.setDefaultButton(true);
submitButton.setPrefWidth(100);
gridPane.add(submitButton, 0, 5, 4, 2);
GridPane.setHalignment(submitButton, HPos.CENTER);
GridPane.setMargin(submitButton, new Insets(20, 0,20,0));
submitButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if(fnamefield.getText().isEmpty()) {
ErrorAlert(Alert.AlertType.ERROR,
gridPane.getScene().getWindow(), "Error!", "Please enter your full name.");
return;
}
if(usernamefield.getText().isEmpty()) {
ErrorAlert(Alert.AlertType.ERROR,
gridPane.getScene().getWindow(), "Error!", "Please enter your username");
return;
}
if(emailfield.getText().isEmpty()) {
ErrorAlert(Alert.AlertType.ERROR,
gridPane.getScene().getWindow(), "Error!", "Please enter your Email");
return;
}
if(passwordfield.getText().isEmpty()) {
ErrorAlert(Alert.AlertType.ERROR,
gridPane.getScene().getWindow(), "Error!", "Please enter your password");
return;
}
String fullname = fnamefield.getText();
String email = emailfield.getText();
String username = usernamefield.getText();
String password = passwordfield.getText();
String all = fullname + "," + email + "," + username + "," + password;
try{
FileWriter fw = new FileWriter ("F:/NEW/1/src/pkg1/Register.txt",true);
PrintWriter out = new PrintWriter (fw);
out.println(all);
out.close();
}
catch (Exception e){
System.out.println("Error " + e);
}
showAlert(Alert.AlertType.CONFIRMATION, gridPane.getScene().getWindow(), "Registration Succesful!", "Welcome " + fnamefield.getText());
}
});
}
//alert box which is called
private void ErrorAlert(Alert.AlertType alertType, Window owner, String
title, String message){
Alert AlertError = new Alert (AlertType.ERROR);
AlertError.setTitle(title);
AlertError.setHeaderText(null);
AlertError.setContentText(message);
AlertError.initOwner(owner);
AlertError.show();
}
private void showAlert(Alert.AlertType alertType, Window owner, String title, String message) {
Alert alert = new Alert(alertType.CONFIRMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.initOwner(owner);
alert.show();
ButtonType b1 = new ButtonType ("Proceed");
ButtonType b2 = new ButtonType ("Cancel");
ButtonType b3 = new ButtonType ("Back to Main Menu");
alert.getButtonTypes().setAll(b1,b2,b3);
if (alert.getResult() == b1) {
new QuestionOne(stage).start(stage);
}else if (alert.getResult() == b2){
new Register(stage).start(stage);
}else {
new Register(stage).start(stage);
//new Main (stage).start(stage);
}
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:1)
设置显示提醒的 按钮类型,然后使用Dialog.showAndWait
至&#34;等待&#34;结果:
...
alert.initOwner(owner);
ButtonType b1 = new ButtonType("Proceed");
ButtonType b2 = new ButtonType("Cancel");
ButtonType b3 = new ButtonType("Back to Main Menu");
alert.getButtonTypes().setAll(b1,b2,b3);
Optional<ButtonType> result = alert.showAndWait();
ButtonType resButton = result.orElse(null);
if (resButton == b1) {
...
} else if (resButton == b2) {
...
} else if (resButton == b3) {
...
}
同样如前所述:没有不带参数的构造函数的Application
类无法启动。您的main
方法无法正常使用。