By GokiSoft.com| 20:43 08/08/2022|
Java Advanced

[Source Code] Tìm hiểu về Java FX - C2108L

#book.css


/*
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
*/
/* 
    Created on : Aug 8, 2022, 8:27:44 PM
    Author     : QTA
*/

#AnchorPane {
    -fx-background-color: orange !important;
}

#author-id {
    -fx-font-size: 50px !important;
    -fx-border-style: dotted;
    -fx-border-color: red;
    -fx-background-image: url(https://cdn.tutsplus.com/cdn-cgi/image/width=360/active/uploads/legacy/tuts/008_colorPicker/Tutorial/8.jpg);
}


#BookLayout.fxml


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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>

<AnchorPane id="AnchorPane" prefHeight="537.0" prefWidth="760.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.gokisoft.BookLayoutController">
   <children>
      <Pane layoutX="14.0" layoutY="14.0" prefHeight="249.0" prefWidth="731.0" style="-fx-background-color: #c0fad0;">
         <children>
            <Label layoutX="38.0" layoutY="27.0" text="Book Name:">
               <font>
                  <Font name="System Bold" size="15.0" />
               </font>
            </Label>
            <TextField fx:id="txtBookName" layoutX="163.0" layoutY="25.0" prefHeight="25.0" prefWidth="512.0" />
            <Button fx:id="saveBtn" layoutX="163.0" layoutY="169.0" mnemonicParsing="false" onAction="#onClickSaveBook" text="Save Book" textFill="#28cdee" />
            <TextField fx:id="txtAuthor" layoutX="163.0" layoutY="79.0" prefHeight="25.0" prefWidth="512.0" />
            <Label layoutX="38.0" id="author-id" layoutY="79.0" text="Author:">
               <font>
                  <Font size="16.0" />
               </font>
            </Label>
            <Button fx:id="resetBtn" layoutX="265.0" layoutY="169.0" mnemonicParsing="false" onAction="#onClickSaveBook" text="Reset" />
         </children>
      </Pane>
   </children>
</AnchorPane>


#BookLayoutController.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gokisoft;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

/**
 * FXML Controller class
 *
 * @author QTA
 */
public class BookLayoutController implements Initializable {
    @FXML
    private TextField txtBookName;
    
    @FXML
    private TextField txtAuthor;
    
    @FXML
    private Button saveBtn;
    
    @FXML
    private Button resetBtn;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    @FXML
    private void onClickSaveBook(ActionEvent event) {
        if(event.getSource() == saveBtn) {
            System.out.println("Click save button");
            String bookName = txtBookName.getText();
            String authroName = txtAuthor.getText();
            
            System.out.println(bookName + ", " + authroName);
        } else if(event.getSource() == resetBtn) {
            System.out.println("Click reset button");
            txtBookName.setText("");
            txtAuthor.setText("");
        }
    }
    
}


#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gokisoft;

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

/**
 * Scene Builder: https://gluonhq.com/products/scene-builder/#download
 * @author QTA
 */
public class Main extends Application{
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("BookLayout.fxml"));
        
        Scene scene = new Scene(root);
        
        scene.getStylesheets().add(getClass().getResource("book.css").toExternalForm());
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
}


Tags:

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

5

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