本节概要
在上一节中实现了图书维护管理,而在本节将实现关于软件的界面。
界面设计
在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】获取本节源码。