Java實戰之圖書管理系統(JavaFX版)(10)——其他界面及功能實現

本節概要

在上一節中實現了圖書維護管理,而在本節將實現關於軟件的界面。

 

界面設計

在view包下創建softInformationFrame.fxml文件,使用Scene Builder設計界面。

該FXML界面的代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
​
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="285.0" prefWidth="450.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="BookManageSystem.controller.SoftInformationFrameController">
    <children>
        <VBox alignment="CENTER" prefHeight="285.0" prefWidth="450.0">
            <children>
                <HBox prefHeight="251.0" prefWidth="510.0">
                    <children>
                        <ImageView fx:id="imageView" cacheHint="SPEED" depthTest="ENABLE" fitHeight="150.0"
                                   fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
                        </ImageView>
                        <VBox alignment="TOP_CENTER" prefHeight="169.0" prefWidth="355.0" spacing="20.0">
                            <children>
                                <Label text="惰惰龜圖書管理系統">
                                    <font>
                                        <Font size="32.0"/>
                                    </font>
                                </Label>
                                <Label text="版本 1.0">
                                    <font>
                                        <Font size="31.0"/>
                                    </font>
                                </Label>
                                <Hyperlink fx:id="hyperlink" alignment="CENTER" focusTraversable="false"
                                           onAction="#hyperlinkEvent" text="相關GitHub鏈接" textAlignment="CENTER"
                                           textOverrun="CLIP" underline="true">
                                    <font>
                                        <Font size="21.0"/>
                                    </font>
                                </Hyperlink>
                                <HBox alignment="CENTER_RIGHT" prefHeight="100.0" prefWidth="200.0">
                                    <children>
                                        <Button fx:id="closeButton" mnemonicParsing="false" onAction="#closeButtonEvent"
                                                text="關閉">
                                            <font>
                                                <Font size="20.0"/>
                                            </font>
                                            <HBox.margin>
                                                <Insets right="50.0"/>
                                            </HBox.margin>
                                        </Button>
                                    </children>
                                </HBox>
                            </children>
                        </VBox>
                    </children>
                </HBox>
            </children>
        </VBox>
    </children>
</AnchorPane>

 

處理控制器

複製Scene Builder中的控制器事件代碼。

在controller包下創建SoftInformationFrameController.java類,將剛纔複製的代碼複製到該類中。

package BookManageSystem;
​
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.image.ImageView;
​
public class SoftInformationFrameController {
​
    @FXML
    private Hyperlink hyperlink;
​
    @FXML
    private Button closeButton;
​
    @FXML
    private ImageView imageView;
​
    @FXML
    void hyperlinkEvent(ActionEvent event) {
​
    }
​
    @FXML
    void closeButtonEvent(ActionEvent event) {
​
    }
​
}

對各個事件處理代碼如下:

package BookManageSystem.controller;
​
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Hyperlink;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
​
import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
​
public class SoftInformationFrameController {
    private Stage dialogStage;
​
    public Stage getDialogStage() {
        return dialogStage;
    }
​
    public void setDialogStage(Stage dialogStage) {
        this.dialogStage = dialogStage;
    }
​
    @FXML
    private Hyperlink hyperlink;
​
    @FXML
    private ImageView imageView;
​
    @FXML // 初始化界面
    public void initialize() {
        // 初始化鏈接組件的超鏈接
        hyperlink.setText("相關GitHub鏈接");
        // 設置圖片
        imageView.setImage(new Image("file:src/BookManageSystem/images/panda.png"));
    }
​
    // “關閉”按鈕的事件監聽器
    public void closeButtonEvent(ActionEvent event) {
        dialogStage.close();
    }
​
    // 超鏈接的事件監聽器
    public void hyperlinkEvent(ActionEvent event) throws URISyntaxException, IOException {
        // 調用電腦本地的瀏覽器打開網址
        Desktop.getDesktop().browse(new URI("https://github.com/lck100/JavaExerciseProject/tree/master/1.%E7%AE%A1%E5%AE%B6%E5%A9%86%E7%B3%BB%E7%BB%9F/%E7%AE%A1%E5%AE%B6%E5%A9%86%E7%B3%BB%E7%BB%9F%EF%BC%88JavaFX%E7%89%88%EF%BC%89"));
    }
}

接着就是加載此FXML界面,在MainApp.java中創建initAboutSoftFrame方法加載FXML文件。

    /**
     * 關於軟件彈出框界面
     * @return 返回Scene
     */
    public Scene initAboutSoftFrame() {
        try {
            // 加載關於軟件界面
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("view/softInformationFrame.fxml"));
            AnchorPane page = (AnchorPane) loader.load();
​
            Stage mainFrameStage = new Stage();
            mainFrameStage.setTitle("關於軟件");
            mainFrameStage.setResizable(true);
            mainFrameStage.setAlwaysOnTop(false);
            mainFrameStage.initModality(Modality.APPLICATION_MODAL);
            mainFrameStage.initOwner(primaryStage);
            Scene scene = new Scene(page);
            mainFrameStage.setScene(scene);
​
            SoftInformationFrameController controller = loader.getController();
            controller.setDialogStage(mainFrameStage);
​
            mainFrameStage.showAndWait();
            return scene;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

接着是在MainFrameController.java中調用該方法,即“關於軟件”菜單項的點擊事件中進行調用。

    public void do_aboutSoftMenuItem_event(ActionEvent event) {
        // 當點擊“關於軟件”菜單項後,加載彈出框
        new MainApp().initAboutSoftFrame();
    }

運行項目點擊“關於軟件”菜單項。

 

總結

前面出了圖書管理系統(swing版)的文章,這個是關於JavaFX版的。在內部邏輯,即數據庫的增刪改查代碼基本是一致的,區別就是界面代碼的實現以及事件的處理,這是swing和JavaFX的區別。

在本項目中已經講了JavaFX的大多數實現,已經可以單獨完成一個項目了,如果對更多的JavaFX感興趣,可以關注JavaFX中文文檔

 

可搜索微信公衆號【Java實例程序】或者掃描下方二維碼關注公衆號獲取更多。

注意:在公衆號後臺回覆【20200305】獲取本節源碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章