如何在Java FX中将值从一个页面传递到另一个页面 [英] How can i pass value from one page to another in Java FX
问题描述
有人可以建议,我们如何将值从一个控制器传递到另一个控制器。
我的场景在下面
登录后我需要在我的主页上获取用户名。
请分享这段代码。
提前致谢!!!
正如我从您的问题中所理解的那样,您需要一个登录屏幕,然后需要连续的其他屏幕进行导航。第一个用户必须登录,验证后,用户信息将进入主屏幕。以下是可能对您有帮助的代码 -
Main.java
包裹样本;
import javafx.application.Application;
import javafx.stage.Stage;
公共类Main扩展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;
公共类LoginController {
私人父母;
私密场景场景;
私人舞台舞台;
@FXML
private TextField userName;
@FXML
private TextField passwordField;
私人家庭控制器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 =新的HomeController();
homeController.redirectHome(stage,userName.getText()。trim());
}
}
public void launchLogingController(阶段阶段){
this.stage = stage;
stage.setTitle(用户登录);
stage.setScene(场景);
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;
公共类HomeController {
私人父母;
私密场景场景;
私人舞台舞台;
@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.setTitle(Home);
stage.setScene(场景);
welcomeText.setText(Hello+ name +!欢迎您。);
stage.hide();
stage.show();
}
}
home.fxml
<?xml version =1.0encoding =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 =CENTERhgap =10.0vgap =10.0xmlns:fx =http://javafx.com/fxml>
< padding>
< Insets bottom =10.0left =25.0right =25.0top =25.0/>
< / padding>
< Text fx:id =welcomeTexttext =homeGridPane.columnIndex =0GridPane.columnSpan =2GridPane.rowIndex =0/>
< / GridPane>
login.fxml
<?xml version =1.0encoding =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 =CENTERhgap =10.0vgap =10.0xmlns:fx =http://javafx.com/fxml>
< padding>
< Insets bottom =10.0left =25.0right =25.0top =25.0/>
< / padding>
< Text text =LoginGridPane.columnIndex =0GridPane.columnSpan =2GridPane.rowIndex =0/>
< Label text =用户名:GridPane.columnIndex =0GridPane.rowIndex =1/>
< TextField fx:id =userNameGridPane.columnIndex =1GridPane.rowIndex =1/>
< Label text =Password:GridPane.columnIndex =0GridPane.rowIndex =2/>
< PasswordField fx:id =passwordFieldGridPane.columnIndex =1GridPane.rowIndex =2/>
< HBox alignment =BOTTOM_RIGHTspacing =10.0GridPane.columnIndex =1GridPane.rowIndex =4>
< Button onAction =#handleSubmitButtonActiontext =登录/>
< / HBox>
< / GridPane>
使用Maven会更好地解决问题。
谢谢!
Can someone suggest ,how can we pass the value from one controller to another controller . My scenario is below
I need to get the username on my home page after login. please share the piece of code.
Thanks in Advance!!!
As I understood from your question, you need a login screen and then other screens in a row for navigation. First user has to sign in and after validating then the user information will go to the Home screen. Below is the codes which may help you -
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>
Using Maven will be better for tackling problems.
Thanks!
这篇关于如何在Java FX中将值从一个页面传递到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!