By GokiSoft.com| 15:35 28/07/2023|
Java Advanced

[Share Code] Tìm hiểu Java FX - C2209I

#pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.gokisoft</groupId>
    <artifactId>C2209IFX02</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>13</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>13</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.4</version>
                <configuration>
                    <mainClass>com.gokisoft.project.App</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <!-- Default configuration for running -->
                        <!-- Usage: mvn clean javafx:run -->
                        <id>default-cli</id>
                    </execution>
                    <execution>
                        <!-- Configuration for manual attach debugging -->
                        <!-- Usage: mvn clean javafx:run@debug -->
                        <id>debug</id>
                        <configuration>
                            <options>
                                <option>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=localhost:8000</option>
                            </options>
                        </configuration>
                    </execution>
                    <execution>
                        <!-- Configuration for automatic IDE debugging -->
                        <id>ide-debug</id>
                        <configuration>
                            <options>
                                <option>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address}</option>
                            </options>
                        </configuration>
                    </execution>
                    <execution>
                        <!-- Configuration for automatic IDE profiling -->
                        <id>ide-profile</id>
                        <configuration>
                            <options>
                                <option>${profiler.jvmargs.arg1}</option>
                                <option>${profiler.jvmargs.arg2}</option>
                                <option>${profiler.jvmargs.arg3}</option>
                                <option>${profiler.jvmargs.arg4}</option>
                                <option>${profiler.jvmargs.arg5}</option>
                            </options>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

#BaseCRUD.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.project.crud;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *
 * @author teacher
 */
public class BaseCRUD {
    static final String DB_NAME = "c2209i";
    static final String DB_USERNAME = "root";
    static final String DB_PWD = "";
    static Connection conn = null;
    static PreparedStatement statement = null;

    static void connect() {
        try {
            //Ket noi CSDL -> doc du lieu ra
            //B1. Mo ket noi CSDL
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/" + DB_NAME, DB_USERNAME, DB_PWD);
        } catch (SQLException ex) {
        }
    }

    static void disconnect() {
        //B3. Dong ket noi
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException ex) {
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
            }
        }
    }
}

#UserCRUD.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.project.crud;

import com.gokisoft.project.models.User;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author teacher
 */
public class UserCRUD extends BaseCRUD {

    public static User login(String email, String pwd) {
        User user = null;

        connect();

        String sql = "select * from users where email = ? and password = ?";
        try {
            statement = conn.prepareStatement(sql);
            statement.setString(1, email);
            statement.setString(2, pwd);

            ResultSet resultSet = statement.executeQuery();
            
            if(resultSet.next()) {
                user = new User(
                        resultSet.getInt("id"), 
                        resultSet.getString("email"), 
                        resultSet.getString("password"), 
                        resultSet.getString("address")
                );
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserCRUD.class.getName()).log(Level.SEVERE, null, ex);
        }

        disconnect();

        return user;
    }
}

#User.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.project.models;

/**
 * create table users (
	id int PRIMARY KEY AUTO_INCREMENT,
    email varchar(150) not null,
    password varchar(32) not null,
    address varchar(200)
)
 * @author teacher
 */
public class User {
    int id;
    String email;
    String password;
    String address;

    public User() {
    }

    public User(int id, String email, String password, String address) {
        this.id = id;
        this.email = email;
        this.password = password;
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" + "id=" + id + ", email=" + email + ", password=" + password + ", address=" + address + '}';
    }
}

#App.java

package com.gokisoft.project;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

/**
 * JavaFX App
 */
public class App extends Application {

    private static Scene scene;

    @Override
    public void start(Stage stage) throws IOException {
        scene = new Scene(loadFXML("primary"), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    static void setRoot(String fxml) throws IOException {
        scene.setRoot(loadFXML(fxml));
    }

    private static Parent loadFXML(String fxml) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
        return fxmlLoader.load();
    }

    public static void main(String[] args) {
        launch();
    }

}

#PrimaryController.java

package com.gokisoft.project;

import com.gokisoft.project.crud.UserCRUD;
import com.gokisoft.project.models.User;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

/**
 * https://gluonhq.com/products/scene-builder/#download
 * @author teacher
 */
public class PrimaryController {
    @FXML
    private Label errorLabel;
    @FXML
    private TextField emailTxt;
    @FXML
    private TextField pwdTxt;
    
    @FXML
    private void login() throws IOException {
        String email = emailTxt.getText();
        String pwd = pwdTxt.getText();
        
        User user = UserCRUD.login(email, pwd);
        if(user != null) {
            errorLabel.setVisible(false);
            App.setRoot("secondary");
        } else {
            errorLabel.setVisible(true);
            errorLabel.setText("Email hoac Mat Khau khong chinh xac");
        }
        
//        if(email.equalsIgnoreCase("admin") && pwd.equals("123456")) {
//            errorLabel.setVisible(false);
//            App.setRoot("secondary");
//        } else {
//            //Hien thi 1 thong bao
//            errorLabel.setVisible(true);
//            errorLabel.setText("Email hoac Mat Khau khong chinh xac");
//        }
    }
}

#SecondaryController.java

package com.gokisoft.project;

import java.io.IOException;
import javafx.fxml.FXML;

/**
 * https://gluonhq.com/products/scene-builder/#download
 * @author teacher
 */
public class SecondaryController {

    @FXML
    private void switchToPrimary() throws IOException {
        App.setRoot("primary");
    }
}

#UserListController.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/javafx/FXMLController.java to edit this template
 */
package com.gokisoft.project;

/**
 * FXML Controller class
 *
 * @author teacher
 */
public class UserListController {
    
}

#module-info.java

module com.gokisoft.project {
    requires javafx.controls;
    requires javafx.fxml;
    requires java.sql;
    requires java.base;
        
    opens com.gokisoft.project to javafx.fxml;
    exports com.gokisoft.project;
}

#primary.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>

<VBox alignment="CENTER" prefHeight="521.0" prefWidth="450.0" spacing="20.0" styleClass="bg" stylesheets="@style.css" xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.gokisoft.project.PrimaryController">
   <children>
      <Label fx:id="errorLabel" styleClass="title" text="THONG BAO ERROR" textFill="#f50606" visible="false" />
      <TextField fx:id="emailTxt" styleClass="form-control" />
      <TextField fx:id="pwdTxt" styleClass="form-control" />
      <Button fx:id="primaryButton" onAction="#login" styleClass="btn-sccuess" text="DANG NHAP" textFill="#fcfcfc" />
   </children>
   <padding>
      <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
   </padding>
</VBox>

#secondary.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>

<VBox alignment="CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.gokisoft.project.SecondaryController">
    <children>
        <Label text="Secondary View" />
        <Button fx:id="secondaryButton" text="Switch to Primary View" onAction="#switchToPrimary" />
    </children>
    <padding>
        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
    </padding>
</VBox>

#style.css

.bg {
    -fx-background-color: #d7f0f5 !important;
}

.title {
    -fx-font-size: 26px;
}

.btn-sccuess {
    -fx-font-size: 26px;
    -fx-background-color: green;
    -fx-max-width: 300px;
    -fx-min-width: 300px;
}

.form-control {
    -fx-font-size: 26px;
    -fx-max-width: 300px;
}

#user-list.css

/*
 * Empty Stylesheet file.
 */

.mainFxmlClass {

}

#user-list.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.gokisoft.project.UserListController">
</AnchorPane>
Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó