本节概要
本节是倒数第二节了,要完成本程序的软件说明界面和帮助功能。
创建界面
创建一个软件说明界面,即在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】可获取本章的源码。