JavaFX:Main,Controller,FXML之间的参数传递

JavaFX:Main,Controller,FXML之间的参数传递


Controller获取FXML数据
   
   
  1. 【.fxml文件】
  2. <Pane fx:controller = "sample.Controller">
  3. <Button fx:id = "sample"> //设置fx:id属性,在根节点中设置 fx:controller属性
  4. </Pane>
  5. 【Controller类】
  6. @FXML Button sample; //通过@FXML标签获取相应的fx:id节点

Main,Controller之间的数据交换
Main类是通过.fxml文件中转来获取Controller类的数据的;
一般不会在Controller方向获取Main数据,因为Controller的加载是在Main之前的,所以Controller几乎无法获取Main的动态数据,只能获取Main的静态数据;
一般Main和Controller之间的数据交换方法是,通过Main获取Controller的一个实例对象,在Controller中将各种动态数据存放在某个bean中,(或者类私域中,并对外提供set、get方法),以下为了演示方便将数据存在Controller中;
   
根据manager_login.fxml修改ManagerLoginController类,manager_login中<Button layoutX="450.0" layoutY="290.0" mnemonicParsing="false" onAction="#handleBack" prefHeight="40.0" prefWidth="100.0" text="返回" />,The controller 'ManagerLoginController' has no event slot 'handleBack' manager_login代码:<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.PasswordField?> <?import javafx.scene.control.TextField?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.text.Font?> <AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="opera.ManagerLoginController"> <Label layoutX="350.0" layoutY="100.0" text="管理员登录"> <Font size="24.0" /> </Label> <Label layoutX="250.0" layoutY="180.0" text="管理员ID:" /> <TextField fx:id="managerIdField" layoutX="330.0" layoutY="180.0" prefWidth="200.0" /> <Label layoutX="250.0" layoutY="230.0" text="密码:" /> <PasswordField fx:id="managerPasswordField" layoutX="330.0" layoutY="230.0" prefWidth="200.0" /> <Button layoutX="330.0" layoutY="290.0" mnemonicParsing="false" onAction="#handleManagerLogin" prefHeight="40.0" prefWidth="100.0" text="登录" /> <Button layoutX="450.0" layoutY="290.0" mnemonicParsing="false" onAction="#handleBack" prefHeight="40.0" prefWidth="100.0" text="返回" /> </AnchorPane> ManagerLoginController类:package opera; import javafx.event.ActionEvent; // 添加必要的导入 import javafx.fxml.FXML; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; import java.io.IOException; // 添加IOException导入 public class ManagerLoginController extends Controller { @FXML private TextField managerIdField; @FXML private PasswordField managerPasswordField; @FXML public void handleManagerLogin(ActionEvent event) { // 改为public访问权限 try { String managerId = managerIdField.getText().trim(); String password = managerPasswordField.getText().trim(); // 空字段检查 if (managerId.isEmpty() || password.isEmpty()) { showErrorAlert("管理员ID和密码不能为空"); return; } // 硬编码验证(实际项目中应使用数据库) if (validateCredentials(managerId, password)) { switchScene("/view/manager_dashboard.fxml"); } else { showErrorAlert("管理员ID或密码错误"); } } catch (IOException e) { // 已导入IOException showErrorAlert("界面切换失败: " + e.getMessage()); } catch (Exception e) { showErrorAlert("系统错误: " + e.getMessage()); } } // 简单的硬编码验证方法 private boolean validateCredentials(String id, String password) { // 实际开发中应替换为数据库查询 return "admin".equals(id) && "admin123".equals(password); } }
06-13
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值