本節概要
在上一節中實現了圖書類別的維護管理,而在本節將實現圖書界面及圖書記錄的添加。
創建實體類
根據數據庫表tb_book創建實體類。
在beans包創建BookBean.java類,其內容如下:
package BookManageSystem.beans;
public class BookBean {
private int bookId;
private String bookName;
private String bookAuthor;
private String bookAuthorSex;
private float bookPrice;
private String bookDescription;
private String bookTypeId;
public BookBean() { }
public BookBean(int bookId, String bookName, String bookAuthor, String bookAuthorSex, float bookPrice, String bookDescription, String bookTypeId) {
this.bookId = bookId;
this.bookName = bookName;
this.bookAuthor = bookAuthor;
this.bookAuthorSex = bookAuthorSex;
this.bookPrice = bookPrice;
this.bookDescription = bookDescription;
this.bookTypeId = bookTypeId;
}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public String getBookAuthorSex() {
return bookAuthorSex;
}
public void setBookAuthorSex(String bookAuthorSex) {
this.bookAuthorSex = bookAuthorSex;
}
public float getBookPrice() {
return bookPrice;
}
public void setBookPrice(float bookPrice) {
this.bookPrice = bookPrice;
}
public String getBookDescription() {
return bookDescription;
}
public void setBookDescription(String bookDescription) {
this.bookDescription = bookDescription;
}
public String getBookTypeId() {
return bookTypeId;
}
public void setBookTypeId(String bookTypeId) {
this.bookTypeId = bookTypeId;
}
}
Dao層方法
接着就是寫添加記錄到數據庫的方法。
在dao包下創建BookDao.java類,其中代碼如下:
package BookManageSystem.dao;
import java.sql.Connection;
import java.sql.Statement;
public class BookDao {
/**
* 操作結果:根據SQL語句執行數據庫的增刪改操作
*
* @param sql SQL語句
* @return boolean 如果操作數據庫成功返回true,否則返回false
*/
public boolean dataChange(String sql) {
Connection conn = null;
Statement stmt = null;
try {
//獲得數據的連接
conn = JDBCUtils.getConnection();
//獲得Statement對象
stmt = conn.createStatement();
//發送SQL語句
int num = stmt.executeUpdate(sql);
return num > 0;
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.release(stmt, conn);
}
return false;
}
}
裏面只有一個方法即是添加圖書記錄到數據庫表中。
界面設計
接下來就是創建圖書添加界面了,在view包下創建bookAddFrame.fxml文件,使用Scene Builder進行設計,代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="700.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="BookManageSystem.controller.BookAddFrameController">
<children>
<VBox alignment="CENTER" prefHeight="700.0" prefWidth="800.0">
<children>
<HBox alignment="CENTER" prefHeight="93.0" prefWidth="800.0">
<children>
<Label text="圖書添加功能">
<font>
<Font name="System Bold" size="40.0"/>
</font>
</Label>
</children>
</HBox>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" spacing="32.0">
<children>
<Label text="圖書名稱:"/>
<TextField fx:id="bookNameTextField" prefHeight="30.0" prefWidth="185.0"/>
<Label text="圖書作者:"/>
<TextField fx:id="bookAuthorTextField" prefHeight="30.0" prefWidth="120.0"/>
</children>
<padding>
<Insets left="80.0"/>
</padding>
<opaqueInsets>
<Insets/>
</opaqueInsets>
</HBox>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" spacing="20.0">
<children>
<Label text="作者性別:"/>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="208.0" spacing="60.0">
<children>
<RadioButton fx:id="maleRadioButton" mnemonicParsing="false" text="男">
<toggleGroup>
<ToggleGroup fx:id="sex"/>
</toggleGroup>
</RadioButton>
<RadioButton fx:id="femaleRadioButton" mnemonicParsing="false" text="女"
toggleGroup="$sex"/>
</children>
</HBox>
<Label text="圖書價格:"/>
<TextField fx:id="bookPriceTextField" prefHeight="30.0" prefWidth="122.0">
<HBox.margin>
<Insets left="10.0"/>
</HBox.margin>
</TextField>
</children>
<padding>
<Insets left="80.0"/>
</padding>
<opaqueInsets>
<Insets/>
</opaqueInsets>
</HBox>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" spacing="40.0">
<children>
<Label text="圖書類別:"/>
<ComboBox fx:id="bookTypeComboBox" prefHeight="30.0" prefWidth="174.0"/>
</children>
<padding>
<Insets left="80.0"/>
</padding>
<opaqueInsets>
<Insets/>
</opaqueInsets>
</HBox>
<HBox prefHeight="100.0" prefWidth="200.0" spacing="40.0">
<children>
<Label text="圖書描述:"/>
<TextArea fx:id="bookDescriptionTextArea" prefHeight="100.0" prefWidth="436.0"/>
</children>
<padding>
<Insets left="80.0"/>
</padding>
<opaqueInsets>
<Insets/>
</opaqueInsets>
</HBox>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="100.0">
<children>
<Button fx:id="addButton" mnemonicParsing="false" onAction="#do_addButton_event" text="添加"/>
<Button fx:id="resetButton" mnemonicParsing="false" onAction="#do_resetButton_event" text="重置"/>
</children>
<padding>
<Insets left="20.0"/>
</padding>
</HBox>
</children>
</VBox>
</children>
</AnchorPane>
並且在controller包下創建BookAddFrameController.java類,從Scene Builder中複製控制器代碼到該類中,並實例化SimpleTools類:
package BookManageSystem.controller;
import BookManageSystem.beans.BookTypeBean;
import BookManageSystem.dao.BookDao;
import BookManageSystem.dao.BookTypeDao;
import BookManageSystem.tools.SimpleTools;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import java.util.List;
public class BookAddFrameController {
private SimpleTools simpleTools = new SimpleTools();
@FXML
private TextField bookAuthorTextField;
@FXML
private RadioButton femaleRadioButton;
@FXML
private TextField bookPriceTextField;
@FXML
private ComboBox<?> bookTypeComboBox;
@FXML
private RadioButton maleRadioButton;
@FXML
private Button addButton;
@FXML
private Button resetButton;
@FXML
private TextField bookNameTextField;
@FXML
private TextArea bookDescriptionTextArea;
// 【添加】按鈕的事件監聽器
public void do_addButton_event(ActionEvent event) {
}
// 【重置】按鈕的事件監聽器
public void do_resetButton_event(ActionEvent event) {
}
}
界面設計完畢後就是加載該FXML所生成的界面。
在MainApp.java中添加如下方法加載FXML文件:
/**
* 圖書添加界面
* @return 返回一個AnchorPane便於其他控件調用
*/
public AnchorPane initBookAddFrame() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("view/bookAddFrame.fxml"));
AnchorPane pane = loader.load();
return pane;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
並在MainFrameController.java處理“圖書添加”菜單項事件中進行調用:
/**
* “圖書添加”菜單項的事件處理
* @param event 事件
*/
public void do_bookAddMenuItem_event(ActionEvent event) {
// 當點擊“圖書添加”菜單項後,加載圖書添加面板
AnchorPane pane = new MainApp().initBookAddFrame();
// 清空界面上原有的控件
mainFrameAnchorPane.getChildren().clear();
// 將圖書添加面板添加到界面上
mainFrameAnchorPane.getChildren().add(pane);
}
運行項目,界面如下:
實現功能
爲界面按鈕添加圖標和初始化下拉列表框的選項,在BookAddFrameController.java中添加initialize方法來完成。
/**
* 初始化界面控件數據
*/
public void initialize() {
// 批量爲按鈕添加圖標
simpleTools.setLabeledImage(new Labeled[]{addButton, resetButton}, new String[]{"src/BookManageSystem/images/add.png",
"src/BookManageSystem/images/reset.png"});
// 查詢所有圖書類別的SQL語句
String getBookTypeSQL = "select * from tb_booktype";
// 獲取所有圖書類別的數據
List bookTypeList = new BookTypeDao().getRecordsDataBySql(getBookTypeSQL);
// 獲取所有的圖書類別名稱
String[] typeNames = new String[bookTypeList.size()];
for (int i = 0; i < bookTypeList.size(); i++) {
BookTypeBean bookTypeBean = (BookTypeBean) bookTypeList.get(i);
typeNames[i] = bookTypeBean.getBookTypeName();
}
// 初始化下拉列表框的選項
simpleTools.addComboBoxItems(bookTypeComboBox, typeNames);
}
運行效果如下:
添加按鈕的事件處理代碼如下:
// 【添加】按鈕的事件監聽器
public void do_addButton_event(ActionEvent event) {
// 獲取用戶輸入的圖書名稱
String name = bookNameTextField.getText();
// 獲取用戶輸入的圖書作者
String author = bookAuthorTextField.getText();
// 獲取用戶選中的圖書作者性別
String sex = "";
if (maleRadioButton.isSelected()) {
sex = maleRadioButton.getText();
} else if (femaleRadioButton.isSelected()) {
sex = femaleRadioButton.getText();
}
// 獲取用戶輸入的圖書價格
String price = bookPriceTextField.getText();
// 獲取用戶選擇的圖書類別
String type = (String) bookTypeComboBox.getSelectionModel().selectedItemProperty().getValue();
// 獲取用戶輸入的圖書描述
String description = bookDescriptionTextArea.getText();
// 條件查詢圖書類別
String bookTypeSQL = "select * from tb_booktype where btName='" + type + "';";
List bookTypeList = new BookTypeDao().getRecordsDataBySql(bookTypeSQL);
BookTypeBean bookTypeBean = (BookTypeBean) bookTypeList.get(0);
// 獲取圖書類別的id號
int bookTypeId = bookTypeBean.getBookTypeId();
// 組裝添加數據的SQL語句
String sql =
"insert into tb_book(bBookName, bAuthor, bSex, bPrice, bBookDescription, btId) values('" + name + "'," +
"'" + author + "','" + sex + "'," + price + ",'" + description + "'," + bookTypeId + ");";
// 執行添加操作並獲取返回結果
boolean isOK = new BookDao().dataChange(sql);
// 對結果進行判斷
if (isOK) {
// 添加成功則重置用戶輸入並彈出提示框
resetContent();
simpleTools.informationDialog(Alert.AlertType.INFORMATION, "提示", "信息", "添加成功!");
} else {
// 添加失敗也彈出提示框
simpleTools.informationDialog(Alert.AlertType.ERROR, "提示", "錯誤", "添加失敗!");
}
}
重置按鈕的事件處理代碼如下:
// 【重置】按鈕的事件監聽器
public void do_resetButton_event(ActionEvent event) {
// 重置用戶輸入及選擇
resetContent();
}
/**
* 重置文本框、單選按鈕和下拉列表框
*/
private void resetContent() {
simpleTools.clearTextField(bookNameTextField, bookAuthorTextField, bookPriceTextField, bookDescriptionTextArea);
simpleTools.clearSelectedRadioButton(maleRadioButton, femaleRadioButton);
simpleTools.clearSelectedComboBox(bookTypeComboBox);
}
即清空用戶輸入和選擇。
可搜索微信公衆號【Java實例程序】或者掃描下方二維碼關注公衆號獲取更多。
注意:在公衆號後臺回覆【20200229】獲取本節源碼。