当前位置: 首页 > news >正文

使用JavaFx Fxml笔记

使用JavaFx Fxml实现账号密码登录 

HelloApplication.java:
package com.example.dr295cmonth7;import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;import java.io.IOException;
import java.util.Objects;public class HelloApplication extends Application {//private 私有对象private Stage primaryStage;@Overridepublic void start(Stage stage) throws IOException {//赋值this.primaryStage = stage;FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("table.fxml"));Scene scene = new Scene(fxmlLoader.load(), 0, 0);scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("assets/styles.css")).toExternalForm());stage.setTitle("DR295C数据采集传输仪");stage.setScene(scene);stage.show();}public static void main(String[] args) {launch();}public void showSuccessDialogAndNavigate() {Alert alert3 = new Alert(Alert.AlertType.INFORMATION);alert3.setTitle("登录成功");alert3.setHeaderText("欢迎登录!");alert3.setContentText("即将跳转至主页面");alert3.showAndWait().ifPresent(response -> {if (response == ButtonType.OK) {//点击确定之后的逻辑} else {// 用户点击了取消按钮或关闭了对话框的处理逻辑System.out.println("用户点击了取消按钮或关闭了对话框");}});}
}
调起其他页面方法
// HelloApplication helloApplication = new HelloApplication();
// helloApplication.showSuccessDialogAndNavigate();

login.fxml:

<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?><GridPane xmlns:fx="http://javafx.com/fxml"fx:controller="com.example.dr295cmonth7.LoginView" alignment="center" hgap="10" vgap="10"><VBox alignment="CENTER" maxWidth="300.0" prefWidth="300.0" spacing="10.0"><HBox alignment="CENTER" maxWidth="300.0" prefWidth="300.0" spacing="10.0"><Label text="登录" GridPane.columnIndex="0" GridPane.rowIndex="0"/></HBox><HBox alignment="CENTER" maxWidth="300.0" prefWidth="300.0" spacing="10.0"><Label text="账号:" GridPane.columnIndex="0" GridPane.rowIndex="0"/><TextField fx:id="usernameField" GridPane.columnIndex="1" GridPane.rowIndex="0"/></HBox><HBox alignment="CENTER" maxWidth="300.0" prefWidth="300.0" spacing="10.0"><Label text="密码:" GridPane.columnIndex="0" GridPane.rowIndex="1"/><PasswordField fx:id="passwordField" GridPane.columnIndex="1" GridPane.rowIndex="1"/></HBox><Label fx:id="welcomeText" alignment="CENTER" maxWidth="1.7976931348623157E308"style="-fx-text-fill: red;"/><Button text="登录" onAction="#handleLogin" GridPane.columnIndex="1" GridPane.rowIndex="2"/></VBox>
</GridPane>

 handleLogin.java:

package com.example.dr295cmonth7;import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;import java.util.Objects;public class LoginView {@FXMLprivate TextField usernameField;@FXMLprivate PasswordField passwordField;@FXMLprivate Label welcomeText;@FXMLprivate Button jumpButton;@FXMLprivate void handleLogin() throws Exception {String usernameValue = usernameField.getText();String passwordValue = passwordField.getText();if (usernameValue.isEmpty() || passwordValue.length() < 6) {Alert alert = new Alert(Alert.AlertType.ERROR);alert.setTitle("提示");alert.setHeaderText("账号和密码不能为空,密码长度至少为 6 位");alert.showAndWait();welcomeText.setText("账号和密码不能为空,密码长度至少为 6 位");return;}if ("admin".equals(usernameValue) && "123456".equals(passwordValue)) {//跳转页面Parent subPage = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("table.fxml")));Scene subPageScene = new Scene(subPage);Stage stage = (Stage) jumpButton.getScene().getWindow();stage.setScene(subPageScene);stage.show();//调起其他页面方法// HelloApplication helloApplication = new HelloApplication();// helloApplication.showSuccessDialogAndNavigate();} else {// 登录失败的提示Alert alert = new Alert(Alert.AlertType.ERROR);alert.setTitle("错误");alert.setHeaderText("账号或密码错误");alert.showAndWait();}}
}

使用JavaFx Fxml实现开始结束时间:

首先创建 TimeRange.fxml 文件:

<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?><HBox xmlns:fx="http://javafx.com/fxml/1" spacing="10"><Label text="创建日期"/><DatePicker fx:id="startDatePicker" prefWidth="140.0"/><Label text="-"/><DatePicker fx:id="endDatePicker" prefWidth="140.0"/><Button onAction="#query" text="查询" textFill="WHITE" GridPane.columnIndex="2" GridPane.rowIndex="0" styleClass="button-all-common" />
</HBox>

然后创建 TimeRangeController.java 类:

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.fxml.FXML;
import javafx.scene.control.DatePicker;import java.time.LocalDate;public class TimeRangeController {private ObjectProperty startDate = new SimpleObjectProperty();@FXMLprivate DatePicker startDatePicker;private ObjectProperty endDate = new SimpleObjectProperty();@FXMLprivate DatePicker endDatePicker;//以后七天:LocalDate.now().plusDays(7)  ;前七天:LocalDate.now().minusDays(7)public TimeRangeController() {// 初始化日期属性 方法一:startDate.set(LocalDate.now().minusDays(7));endDate.set(LocalDate.now());// 初始化日期属性 方法二:
//        LocalDate today = LocalDate.now();
//        startDate.set(today.minusDays(7));
//        endDate.set(today);}@FXMLpublic void initialize() {startDatePicker.valueProperty().bindBidirectional(startDate);endDatePicker.valueProperty().bindBidirectional(endDate);}//查询@FXMLvoid query(ActionEvent event) {System.out.println(startDate.get());   //开始时间System.out.println(endDate.get());   //结束时间}}

使用JavaFx Fxml定义各种类型的数据:

字符串 + 整数 :
private StringProperty searchValue = new SimpleStringProperty();
@FXML
private TextField searchField;

表格:

@FXML
private TableView<User> operateTableView;@FXML
private TableColumn<User, String> startTime;@FXML
private TableColumn<User, String> endTime;@FXML
private TableColumn<User, String> operator;

 

 日期:

private ObjectProperty startDate = new SimpleObjectProperty();
@FXML
private DatePicker startDatePicker;

 开关

@FXML
    private CheckBox booleanCheckBox;

http://www.lryc.cn/news/410902.html

相关文章:

  • 友盟U-APM——优秀的前端性能监控工具
  • 人工智能与机器学习原理精解【10】
  • TypeScript 简介
  • 什么是知识库?为什么我需要一个?
  • MySQL学习(16):视图
  • android13关机按钮 去掉长按事件 去掉启动到安全模式 删除关机长按
  • 递归求数组和
  • MySQL数据库介绍
  • 向量数据库性能测试工具(VectorDBBench.com)性价比排名
  • 2024年的AI人工智能风口是Python?一篇文章告诉你为什么!
  • 使用SpringBoot集成CAS、应用场景和示例代码
  • python爬取某财富网
  • Python 【机器学习】 进阶 之 【实战案例】房价数据中位数分析 之 [ 选择并训练模型 ] [ 模型微调 ] | 3/3(含分析过程)
  • NLP-使用Word2vec实现文本分类
  • 基于SpringBoot实现验证码功能
  • 字节测开面筋大总结!!!!
  • Mindspore框架DCGAN模型实现漫画头像生成|(二)DCGAN模型构建
  • mongo-csharp-driver:MongoDB官方的C#客户端驱动程序!
  • 网络流量分析>>pcapng文件快速分析有用价值解析
  • 【大模型系列篇】Vanna-ai基于检索增强(RAG)的sql生成框架
  • 【Nacos安装】
  • js、ts、argular、nodejs学习心得
  • 【Unity】RPG2D龙城纷争(十八)平衡模拟器
  • java.lang.IllegalStateException: Duplicate key InventoryDetailDO
  • Python使用selenium访问网页完成登录——装饰器重试机制汇总
  • “微软蓝屏”事件引发的深度思考:网络安全与系统稳定性的挑战与应对
  • 2024.07纪念一 debezium : spring-boot结合debezium
  • mysql怎么查询json里面的字段
  • C++ 右值 左值引用
  • 「JavaEE」Spring IoC 1:Bean 的存储