Java实战之管家婆记账系统(9)——删除账目界面及功能实现

本节概要

本节将实现账目记录的删除。

创建界面

使用JavaFX创建界面,在view包下创建deleteAccountFrame.fxml,用Scene Builder设计界面,其中各控件属性和事件参考下面代码:

<?xml version="1.0" encoding="UTF-8"?>
​
​
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="AccountSystem.controller.DeleteAccountFrameController">
    <children>
        <BorderPane prefHeight="400.0" prefWidth="600.0">
            <top>
                <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
                    <children>
                        <Label text="序号:"/>
                        <TextField fx:id="idTextField" promptText="请填入账目记录的序号:"/>
                    </children>
                </HBox>
            </top>
            <bottom>
                <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="50.0"
                      BorderPane.alignment="CENTER">
                    <children>
                        <Button fx:id="checkButton" mnemonicParsing="false" onAction="#checkButtonEvent" text="查询"/>
                        <Button fx:id="deleteButton" mnemonicParsing="false" onAction="#deleteButtonEvent" text="删除"/>
                    </children>
                </HBox>
            </bottom>
            <center>
                <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
                    <children>
                        <Label fx:id="contentLabel"/>
                    </children>
                </HBox>
            </center>
        </BorderPane>
    </children>
</AnchorPane>

接着就是创建与之相对应的控制器类,在controller包下创建DeleteAccountFrameController.java类,并从Scene Builder中复制基本控件代码到该类中:

package AccountSystem.controller;
​
import AccountSystem.bean.Record;
import AccountSystem.bean.Session;
import AccountSystem.dao.RecordDao;
import AccountSystem.tools.SimpleTools;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
​
/**
 * 删除账目界面控制器
 *
 * @author lck100
 */
public class DeleteAccountFrameController {
    @FXML
    private TextField idTextField;
​
    @FXML
    private Label contentLabel;
​
    /**
     * ”查询“按钮的事件监听器
     *
     * @param actionEvent 事件
     */
    @FXML
    public void checkButtonEvent(ActionEvent actionEvent) {
       
    }
​
    /**
     * ”删除“按钮的事件监听器
     *
     * @param actionEvent 事件
     */
    @FXML
    public void deleteButtonEvent(ActionEvent actionEvent) {
       
    }
}

接下来就是加载FXML文件。在MainApp.java中添加如下方法加载deleteAccountFrame.fxml界面:

    /**
     * 操作结果:删除账目界面
     */
    public Scene initDeleteFrame() {
        try {
            Parent page = FXMLLoader.load(getClass().getResource("view/deleteAccountFrame.fxml"));
​
            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);
​
            mainFrameStage.show();
            return scene;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

然后是调用执行该方法,在MainPageController.java中的deleteMenuItemEvent()方法中调用,即删除菜单项的事件处理方法:

    /**
     * ”删除“菜单项的事件监听器
     *
     * @param actionEvent 事件
     */
    @FXML
    public void deleteMenuItemEvent(ActionEvent actionEvent) {
        // 调用删除账目界面
        mainApp.initDeleteFrame();
        // 刷新数据
        initialize();
    }

界面完成,执行程序,运行的界面如下:

实现功能

在删除界面有两个按钮”查询“和”删除“,功能分别是获取用户输入的序号查询要删除的记录,然后点击”删除“按钮删除掉该条记录。

所以查询按钮的事件监听器代码如下:

    /**
     * ”查询“按钮的事件监听器
     *
     * @param actionEvent 事件
     */
    @FXML
    public void checkButtonEvent(ActionEvent actionEvent) {
        // 实例化Record对象
        Record record = new Record();
        // 实例化RecordDao对象
        RecordDao recordDao = new RecordDao();
        // 通过记录ID和用户ID查询账目记录
        Record checkedRecord = recordDao.selectRecordByIdAndUserId(Integer.parseInt(idTextField.getText()), Session.getUser().getUserId());
        String info = "";
        if (checkedRecord.getRecordType() == null && checkedRecord.getRecordClassification() == null) {
            info = "无此查询结果!";
        } else {
            info =
                    "类型:\t\t" + checkedRecord.getRecordType() + "\n"
                            + "金额:\t\t" + checkedRecord.getRecordMoney() + "\n"
                            + "分类:\t\t" + checkedRecord.getRecordClassification() + "\n"
                            + "备注:\t\t" + checkedRecord.getRecordMemo() + "\n"
                            + "日期:\t\t" + checkedRecord.getRecordDate() + "\n";
        }
        // 显示查询结果
        contentLabel.setText(info);
    }

输入序号查询如下:

然后按钮的监听器代码如下:

    /**
     * ”删除“按钮的事件监听器
     *
     * @param actionEvent 事件
     */
    @FXML
    public void deleteButtonEvent(ActionEvent actionEvent) {
        // 将string类型数据转换为int类型数据
        int id = Integer.parseInt(idTextField.getText());
        // 实例化RecordDao对象
        RecordDao recordDao = new RecordDao();
        // 根据ID删除记录
        boolean b = recordDao.deleteRecord(new Record(id));
        if (b) {
            SimpleTools.informationDialog(Alert.AlertType.INFORMATION, "提示", "信息", "删除数据成功!");
            // 删除成功后就清除窗体数据
            idTextField.setText("");
            contentLabel.setText("");
        } else {
            SimpleTools.informationDialog(Alert.AlertType.ERROR, "提示", "错误", "删除数据失败!");
        }
    }

代码解释说明:获取要删除的序号,然后调用RecordDao类中的deleteRecord()方法执行删除操作,并对删除结果进行提示。

运行程序,执行功能如下:

可搜索微信公众号【Java实例程序】或者扫描下方二维码关注公众号获取更多。

注意:在公众号后台回复【20200326】可获取本章的源码 。

发布了500 篇原创文章 · 获赞 77 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/cnds123321/article/details/104280387