gpt4 book ai didi

javafx-2 - 我如何在 Javafx 中将值从一个页面传递到另一个页面

转载 作者:行者123 更新时间:2023-12-02 07:32:59 26 4
gpt4 key购买 nike

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

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

提前致谢!!!

最佳答案

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

Main.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);
}
}

LoginController.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();
}
}

HomeController.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();
}
}

home.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>

login.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 会更好地解决问题。

谢谢!

关于javafx-2 - 我如何在 Javafx 中将值从一个页面传递到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19866175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com