如何识别对象是JavaFX的场景

时间:2018-02-12 20:55:20

标签: java javafx textfield fxml fxmlloader

我在fxml中有两种不同的布局。在它们中都有一个名为usernameFieldpasswordField的TextField。在我的Main课程中,我需要同时使用它们。那么如何识别它们呢?

我的代码:

package application;




import javax.swing.JOptionPane;


import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;


public class Main extends Application {
    int userID;
    @FXML TextField usernameField;
    @FXML PasswordField passwordField;
    @FXML Button btnLogin = new Button();
    @FXML Button btnToRegister = new Button();
    @FXML Button btnClose = new Button();
    @FXML Button btnRegister = new Button();
    Stage window;
    @Override
    public void start(Stage primaryStage) {
        window = primaryStage;

        try {

            FXMLLoader fxmlLogin = new  FXMLLoader(getClass().getResource("Login.fxml"));
            fxmlLogin.setController(this);

            FXMLLoader fxmlUser = new FXMLLoader(getClass().getResource("User.fxml"));
            fxmlUser.setController(this);

            FXMLLoader fxmlRegister = new FXMLLoader(getClass().getResource("Register.fxml"));
            fxmlRegister.setController(this);
            //Button btnRegister = (Button) fxmlRegister.load();
            AnchorPane root = (AnchorPane)fxmlLogin.load();
            AnchorPane PizzaUser = (AnchorPane)fxmlUser.load();


            AnchorPane register = (AnchorPane)fxmlRegister.load();
            Scene sceneUser = new Scene(PizzaUser, 800,500);
            Scene sceneLogin = new Scene(root,800,500);
            Scene sceneRegister = new Scene(register, 550, 425);

            register.setBackground(new Background(new BackgroundImage(new Image("file:/C:\\Users\\LEV_GAMES\\eclipse-workspace\\PizzaSuiteFX\\background register.png",550,425,false,true),
                BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT,
                  BackgroundSize.DEFAULT)));
        root.setBackground(new Background(new BackgroundImage(new Image("file:/C:\\Users\\LEV_GAMES\\eclipse-workspace\\PizzaSuiteFX\\background login.png",800,500,false,true),
                BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT,
                  BackgroundSize.DEFAULT)));

        //scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        window.setScene(sceneLogin);
        window.setTitle("Pizza Suite (Login)");
        primaryStage.show();

        btnRegister.setOnAction(e ->{
            try {
                //here i wanna get the text from usernameField from register 
                System.out.println(usernameField.getText());
                //SQL_methods.SQL.registerUserPassUser(usernameField.getText(), passwordField.getText(), phoneField.getText(), nameField.getText(), adressField.getText(), cityField.getText());
            } catch (Exception e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }

            JOptionPane.showMessageDialog(null, "U bent geregistreerd!","Gelukt!",JOptionPane.OK_OPTION);

        });

        btnClose.setOnAction(e -> {
            window.close();
        });

        btnToRegister.setOnAction(e -> {
        //here i wanna get the text from usernameField from login
        System.out.Println(usernameField.getText());
            window.setScene(sceneRegister);
            window.setTitle("Pizza Suite (Registreren)");
        });

        btnLogin.setOnAction(e -> {             
            String typeuser = "invalid";
            try {
                typeuser = SQL_methods.SQL.loginUserPassArray(usernameField.getText(), passwordField.getText());
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (Throwable e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            if(typeuser.equals("admin")) {
                userID = SQL_methods.SQL.getIDofUser("admins", usernameField.getText(), passwordField.getText());
                passwordField.setText(null);
                usernameField.setText(null);
                //frame.setVisible(false);
                //PizzaAdmin.PizzaA.frame.setVisible(true);
            }
            if(typeuser.equals("user")) {
                userID = SQL_methods.SQL.getIDofUser("customers", usernameField.getText(), passwordField.getText());
                passwordField.setText(null);
                usernameField.setText(null);
                window.setScene(sceneUser);
                window.setTitle("Pizza Suite (User order)");
            }
            if(typeuser.equals("staff")) {
                userID = SQL_methods.SQL.getIDofUser("staff", usernameField.getText(), passwordField.getText());
                usernameField.setText(null);
                passwordField.setText(null);
                //frame.setVisible(false);
                //PizzaStaff.PizzaS.frame.setVisible(true);
            }

            if(typeuser.equals("invalid")) {
                JOptionPane.showMessageDialog(null, "Invalid password and username!", "Error 02", JOptionPane.ERROR_MESSAGE);
            }
        });



    } catch(Exception e) {
        e.printStackTrace();
    }

}


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

}

My first FXML (Login)

My second FXML (Register)

我该如何解决这个问题?

0 个答案:

没有答案