本節概要
本節是倒數第二節了,要完成本程序的軟件說明界面和幫助功能。
創建界面
創建一個軟件說明界面,即在view包下創建softInformationFrame.fxml文件,使用Scene Builder設計界面,該界面的組件屬性和事件方法參考下面的代碼:
<?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.Image?>
<?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="AccountSystem.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">
<image>
<Image url="@../images/panda.png"/>
</image>
</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>
接着是在controller包下創建與之對應的控制器類並從Scene Builder中複製該界面的組件對象和事件方法代碼到該類中:
package AccountSystem.controller;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Hyperlink;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* 關於軟件信息界面控制器
*
* @author lck100
*/
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;
/**
* “關閉”按鈕的事件監聽器
*
* @param event 事件
*/
public void closeButtonEvent(ActionEvent event) {
}
/**
* 超鏈接的事件監聽器
*
* @param event 事件
* @throws URISyntaxException 拋出URISyntaxException
* @throws IOException 拋出IOException
*/
public void hyperlinkEvent(ActionEvent event){
}
}
再接着就是在MainApp.java中寫方法加載FXML資源文件:
/**
* 操作結果:“關於軟件”查詢結果界面
*/
public Scene initSoftInformationFrame() {
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);
// 加載CSS樣式文件
scene.getStylesheets().add(MainApp.class.getResource(getStyleValue()).toExternalForm());
SoftInformationFrameController controller = loader.getController();
controller.setDialogStage(mainFrameStage);
mainFrameStage.showAndWait();
return scene;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
最後就是調用該方法在MainPageController.java中的菜單項事件:
/**
* “關於軟件”菜單項的事件監聽器
*
* @param actionEvent 事件
*/
@FXML
public void abutSoftMenuItemEvent(ActionEvent actionEvent) {
// 打開關於軟件界面
mainApp.initSoftInformationFrame();
}
同時”幫助“菜單項的事件處理如下:
/**
* “幫助”菜單項的事件監聽器
*
* @param actionEvent 事件
*/
@FXML
public void helpMenuItemEvent(ActionEvent actionEvent) 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"));
}
然後是SoftInformationFrameController.java中的事件處理,這控制器類中的代碼沒什麼說的,全部給了:
package AccountSystem.controller;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Hyperlink;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* 關於軟件信息界面控制器
*
* @author lck100
*/
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;
/**
* 初始化界面
*/
public void initialize() {
// 初始化鏈接組件的超鏈接
hyperlink.setText("相關GitHub鏈接");
}
/**
* “關閉”按鈕的事件監聽器
*
* @param event 事件
*/
public void closeButtonEvent(ActionEvent event) {
// 關閉該窗口
dialogStage.close();
}
/**
* 超鏈接的事件監聽器
*
* @param event 事件
* @throws URISyntaxException 拋出URISyntaxException
* @throws IOException 拋出IOException
*/
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"));
}
}
運行程序,測試下:
可搜索微信公衆號【Java實例程序】或者掃描下方二維碼關注公衆號獲取更多。
注意:在公衆號後臺回覆【20200428】可獲取本章的源碼。