使用 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;
 
    @Override
    public 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("用户点击了取消按钮或关闭了对话框");
            }
        });
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.

调起其他页面方法

// 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>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

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 {
 
    @FXML
    private TextField usernameField;
 
    @FXML
    private PasswordField passwordField;
 
    @FXML
    private Label welcomeText;
 
    @FXML
    private Button jumpButton;
    @FXML
    private 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();
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.

使用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>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

然后创建 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();
    @FXML
    private DatePicker startDatePicker;
 
    private ObjectProperty endDate = new SimpleObjectProperty();
    @FXML
    private 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);
    }
 
    @FXML
    public void initialize() {
        startDatePicker.valueProperty().bindBidirectional(startDate);
        endDatePicker.valueProperty().bindBidirectional(endDate);
    }
 
 //查询
    @FXML
    void query(ActionEvent event) {
        System.out.println(startDate.get());   //开始时间
        System.out.println(endDate.get());   //结束时间
    }
 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

使用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;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

设置单元格宽度自适应setColumnResizePolicy

operateTableView.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);
  • 1.