2

有人可以建议,我们如何将值从一个控制器传递到另一个控制器。我的场景如下

登录后我需要在主页上获取用户名。请分享一段代码。

提前致谢!!!

4

1 回答 1

7

正如我从您的问题中了解到的那样,您需要一个登录屏幕,然后连续使用其他屏幕进行导航。第一个用户必须登录,验证后用户信息将进入主屏幕。以下是可能对您有所帮助的代码 -

主.java

package sample;    
import javafx.application.Application;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        LoginController loginController = new LoginController();
        loginController.launchLogingController(primaryStage);
    }


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

登录控制器.java

package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.IOException;


public class LoginController {

    private Parent parent;
    private Scene scene;
    private Stage stage;
    @FXML
    private TextField userName;
    @FXML
    private TextField passwordField;

    private HomeController homeController;

    public LoginController() {
        FXMLLoader fxmlLoader = new          FXMLLoader(getClass().getResource("/fxml/login.fxml"));
        fxmlLoader.setController(this);
        try {
            parent = (Parent) fxmlLoader.load();
            scene = new Scene(parent, 600, 400);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    protected void handleSubmitButtonAction(ActionEvent event) {
        System.out.println(userName.getText());
        if (userName.getText().trim().length() > 0 && passwordField.getText().trim().length() > 0) {
            homeController = new HomeController();
            homeController.redirectHome(stage, userName.getText().trim());
        }

    }

    public void launchLogingController(Stage stage) {
        this.stage = stage;
        stage.setTitle("User Login");
        stage.setScene(scene);
        stage.setResizable(true);
        stage.hide();
        stage.show();
    }
}

家庭控制器.java

 package sample;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;

    import java.io.IOException;

    public class HomeController {
        private Parent parent;
        private Scene scene;
        private Stage stage;
        @FXML
        private Text welcomeText;

        public HomeController() {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/home.fxml"));
            fxmlLoader.setController(this);
            try {
                parent = (Parent) fxmlLoader.load();
                scene = new Scene(parent, 600, 400);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        public void redirectHome(Stage stage, String name) {
            stage.setTitle("Home");
            stage.setScene(scene);
            welcomeText.setText("Hello " + name + "! You are welcome.");
            stage.hide();
            stage.show();
        }
    }

主页.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.text.*?>
<?import sample.LoginController?>

<GridPane alignment="CENTER" hgap="10.0" vgap="10.0" xmlns:fx="http://javafx.com/fxml">
  <padding>
    <Insets bottom="10.0" left="25.0" right="25.0" top="25.0" />
  </padding>
  <Text fx:id="welcomeText" text="home" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="0" />

</GridPane>

登录.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.text.*?>
<?import sample.LoginController?>

<GridPane alignment="CENTER" hgap="10.0" vgap="10.0" xmlns:fx="http://javafx.com/fxml">
  <padding>
    <Insets bottom="10.0" left="25.0" right="25.0" top="25.0" />
  </padding>
  <Text text="Login" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="0" />
  <Label text="User Name:" GridPane.columnIndex="0" GridPane.rowIndex="1" />
  <TextField fx:id="userName" GridPane.columnIndex="1" GridPane.rowIndex="1" />
  <Label text="Password:" GridPane.columnIndex="0" GridPane.rowIndex="2" />
  <PasswordField fx:id="passwordField" GridPane.columnIndex="1" GridPane.rowIndex="2" />
  <HBox alignment="BOTTOM_RIGHT" spacing="10.0" GridPane.columnIndex="1" GridPane.rowIndex="4">
    <Button onAction="#handleSubmitButtonAction" text="Sign In" />
  </HBox>
</GridPane>

使用 Maven 将更好地解决问题。

谢谢!

于 2013-11-13T09:42:57.137 回答