简体   繁体   中英

Stage won't open even though the code runs in javafx

So, I made this simple code and it doesn't show any error or warnings, but for some reason, when I press run, it doesn't open a window. I'm not sure where it went wrong. The point of the code is to show a simple text depending on who tries to log in. I'm going to expand it later and add new things to it, but for now it just needs to run as it is. Here's the code:

public class LogIn extends Application {

    Users Cashier = new Users("Filan", "Fisteku", "ffisteku@gmail.com", 500, 1, "0691234567", "15/01/2000", "open");
    Users Manager = new Users("Mario", "Rossi", "mrossi@gmail.com", 750, 2, "0681234567", "09/08/1995", "lararossi");
    Users Administrator = new Users("John", "Doe", "jdoe1980@gmail.com", 1000, 3, "0697654321", "11/11/1980",
            "password");

    public void start(final Stage stage) {
        // Creating nodes

        final TextField txtField = new TextField();
        final PasswordField pwField = new PasswordField();
        final Button button = new Button("LOGIN");
        button.setTranslateX(250);
        button.setTranslateY(75);
        // Creating labels
        final Label label1 = new Label("Email: ");
        final Label label2 = new Label("Password: ");
        // Setting the message with read data
        final Text text = new Text("");
        Font font = Font.font("Comic Sans", FontWeight.BOLD, FontPosture.REGULAR, 10);
        text.setFont(font);
        text.setTranslateX(15);
        text.setTranslateY(125);
        text.setFill(Color.BLACK);
        // Displaying the message
        button.setOnAction(new EventHandler() {
            public void handle(Event e) {
                // Retrieving data
                final String email = txtField.getText();
                final String pw = pwField.getText();

                if (email.equals(Cashier.email) && pw.equals(Cashier.password)) {
                    text.setText("Hello " + Cashier.name + " " + Cashier.lastname + ". Welcome!");

                }
                if (email.equals(Manager.email) && pw.equals(Manager.password)) {
                    text.setText("Hello " + Manager.name + " " + Manager.lastname + ". Welcome!");

                }
                if (email.equals(Administrator.email) && pw.equals(Administrator.password)) {
                    text.setText("Hello " + Administrator.name + " " + Administrator.lastname + ". Welcome!");

                }
                // Adding labels for nodes
                HBox box = new HBox(5);
                box.setPadding(new Insets(25, 5, 5, 50));
                box.getChildren().add(label1);
                box.getChildren().add(txtField);
                box.getChildren().add(label2);
                box.getChildren().add(pwField);
                Group root = new Group();
                root.getChildren().add(box);
                
// Setting the stage
                Scene scene = new Scene(root, 500, 250, Color.SILVER);
                stage.setTitle("LogIn Site for Tech Store");
                stage.setScene(scene);
                stage.show();
            }
        }

        );
        ;
        ;
        ;
        ;
    }

    public static void main(String args[]) {
        launch(args);
    }

}

Your code,

// Setting the stage
      Scene scene = new Scene(root, 500, 250, Color.SILVER);
      stage.setTitle("LogIn Site for Tech Store");
      stage.setScene(scene);
      stage.show();

should come out of the EventHandler method .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM