本节概要
このセクションでは、会計記録に追加されます。
予備的
次のようにこのセクションで達成レコードを追加するには、そのパッケージにClassificationDao.javaのDAOクラスを作成し、情報のクエリ分類に使用されます。
package AccountSystem.dao;
import AccountSystem.bean.Classification;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class ClassificationDao {
/**
* 通过收入或支出类型获取所有的分类信息
*
* @param classificationType 收入或支出分类
* @return 返回得到的分类信息
*/
public List<Classification> selectByType(String classificationType) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List<Classification> classificationList = new ArrayList<Classification>();
try {
//获得数据的连接
conn = JDBCUtils.getConnection();
//获得Statement对象
stmt = conn.createStatement();
// 拼接SQL语句
String sql = "select * from tb_classification where cType='" + classificationType + "';";
//发送SQL语句
rs = stmt.executeQuery(sql);
while (rs.next()) {
Classification classification = new Classification();
classification.setcId(rs.getInt(1));
classification.setcName(rs.getString(2));
classification.setcType(rs.getString(3));
classificationList.add(classification);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.release(rs, stmt, conn);
}
return classificationList;
}
}
インターフェイスを実装します
まず、JavaFXのに追加された機能を完了する最初のFXMLビューファイルを作成し、その後、MainAppにして作成したロードFXMLビューファイルに関連付けられているコントローラクラスコントローラー、そしてメソッドを作成し、最終的にイベントで呼び出す必要があります。 。
ビューでは、パッケージaddAccountFrame.fxmlは、ファイルを作成し、シーンビルダインターフェイスのデザインを使用して、プロパティ、および各制御方式のイベントは、以下のコードを参照してください。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?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.AddAccountFrameController">
<children>
<VBox alignment="CENTER" focusTraversable="true" layoutX="137.0" prefHeight="400.0" prefWidth="326.0"
spacing="20.0">
<children>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
<children>
<Label fx:id="descriptionLabel" text="添加"/>
</children>
</HBox>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0">
<children>
<Label text="类型:"/>
<HBox alignment="CENTER_LEFT" prefHeight="30.0" prefWidth="240.0" spacing="50.0">
<children>
<RadioButton fx:id="outputRadioButton" mnemonicParsing="false"
onAction="#outputRadioButtonEvent" text="支出">
<toggleGroup>
<ToggleGroup fx:id="group"/>
</toggleGroup>
</RadioButton>
<RadioButton fx:id="inputRadioButton" mnemonicParsing="false"
onAction="#inputRadioButtonEvent" text="收入" toggleGroup="$group"/>
</children>
</HBox>
</children>
</HBox>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0">
<children>
<Label text="金额:"/>
<TextField fx:id="moneyTextField" prefHeight="30.0" prefWidth="240.0" promptText="请填入金额:"/>
</children>
</HBox>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0">
<children>
<Label text="分类:"/>
<ComboBox fx:id="classificationComboBox" onAction="#classificationComboBoxEvent"
prefHeight="30.0" prefWidth="240.0" promptText="请选择分类:"/>
</children>
</HBox>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0">
<children>
<Label text="备注:"/>
<TextArea fx:id="memoTextArea" prefHeight="200.0" prefWidth="240.0" promptText="请填入备注:"/>
</children>
</HBox>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0">
<children>
<Label text="日期:"/>
<DatePicker fx:id="datePickerTextField" prefHeight="30.0" prefWidth="240.0"
promptText="请选择日期:"/>
</children>
</HBox>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
<children>
<Button fx:id="addButton" mnemonicParsing="false" onAction="#addButtonEvent" text="添加"/>
</children>
</HBox>
</children>
<opaqueInsets>
<Insets/>
</opaqueInsets>
<padding>
<Insets bottom="20.0"/>
</padding>
</VBox>
</children>
</AnchorPane>
コントローラパッケージのAddAccountFrameController.javaクラスを作成し、クラスの制御方法におけるシーンビルダオブジェクトとイベントからコピーされました:
package AccountSystem.controller;
import AccountSystem.bean.Classification;
import AccountSystem.bean.Record;
import AccountSystem.bean.Session;
import AccountSystem.dao.ClassificationDao;
import AccountSystem.dao.RecordDao;
import AccountSystem.tools.PublicTools;
import AccountSystem.tools.SimpleTools;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import java.util.List;
/**
* 添加账目界面控制器
*
* @author lck100
*/
public class AddAccountFrameController {
private PublicTools publicTools = new PublicTools();
@FXML
private String selectedRadioButton = null;
@FXML
private String selectedCoboboxItem = null;
@FXML
private Label descriptionLabel;
@FXML
private RadioButton outputRadioButton;
@FXML
private DatePicker datePickerTextField;
@FXML
private TextField moneyTextField;
@FXML
private RadioButton inputRadioButton;
@FXML
private TextArea memoTextArea;
@FXML
private ComboBox<?> classificationComboBox;
/**
* “添加”按钮的事件监听器
*
* @param actionEvent 事件
*/
@FXML
public void addButtonEvent(ActionEvent actionEvent) {
}
/**
* “支出”单选按钮的事件监听器
*
* @param actionEvent 事件
*/
public void outputRadioButtonEvent(ActionEvent actionEvent) {
}
/**
* “收入”单选按钮的事件监听器
*
* @param actionEvent 事件
*/
public void inputRadioButtonEvent(ActionEvent actionEvent) {
}
/**
* ”分类“下拉列表框的事件监听器
*
* @param actionEvent 事件
*/
public void classificationComboBoxEvent(ActionEvent actionEvent) {
}
}
コード説明:
あなたは、ボタンのイベントハンドラはよく理解されて追加し、上記の4つのイベントリスナーメソッドがあります見ることができ、ユーザの後にレコードがデータベーステーブルに情報を入力し、彼らが聞きたい、なぜ他の三つの方法が理解できないかもしれとき。「支出」、さまざまな分類がありますので、「支出」と「利益」のラジオボタンのリスナーは、類似しており、これらの分類は、ドロップダウンリストボックスに表示され、すべての「収入」の同じ分類も表示されますドロップダウンリストボックスで、それは収入と支出を分類することは不可能であるが一緒に表示され、設定上の2つのラジオボタンが無意味になり、収入と支出のない部門は存在しません。
ユーザーは「収入」のラジオボタン、すべての所得のカテゴリ名がドロップダウンリストボックスに入力されます選択した際に、ユーザが選択するラジオボタンを「支出」と、それはすべて、以下を埋めるためにカテゴリ名を過ごしますドロップダウンリストボックスで、ユーザーが個別にカテゴリを選択することができます。
ドロップダウンリストボックスに動的イベントを監視するには、ユーザが選択したカテゴリ名を取得することです。
次の変更は、ラジオボタンを聞いて紹介します:
(この時、2つのラジオボタンが選択されていないので、何のドロップダウンリストボックスの値が存在しない)、次のようにポップアップメニューインターフェイスを追加するために「追加」をクリックします:
「支出」のラジオボタン、すべての「費用」カテゴリ名の以下の分類を選択します。
「利益」のラジオボタン、すべての「収入」カテゴリ名の以下の分類を選択します。
コントローラインタフェースビューインターフェイスFXMLビューをロードし、続いて仕上げ、ローディングaddAccountFrame.fxmlファイルMainApp.javaの次のメソッドを追加します。
/**
* 操作结果:添加账目界面
*/
public Scene initAddFrame() {
try {
Parent page = FXMLLoader.load(getClass().getResource("view/addAccountFrame.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に呼び出されます。
/**
* ”添加“菜单项的事件监听器
*
* @param actionEvent 事件
*/
@FXML
public void addMenuItemEvent(ActionEvent actionEvent) {
// 刷新界面数据
initialize();
// 调用添加账目界面
mainApp.initAddFrame();
}
追加したインターフェイスを見ることができるプロジェクトを実行しますが、何のイベントハンドラ、唯一のインタフェースはありません。
機能の追加
次のようにAddAccountFrameController追加機能は、addButtonEvent()メソッドで実装します。
// 类型
String type = selectedRadioButton;
// 金额,把从文本框得到的string类型数据转换为float类型
float money = Float.parseFloat(moneyTextField.getText());
// 分类
String classification = selectedCoboboxItem;
// 备注
String memo = memoTextArea.getText();
// 日期
String date = datePickerTextField.getValue().toString();
// 将用户输入的数据封装到Record实体类中
Record record = new Record(Session.getUser().getUserId(), type, money, classification, memo, date);
// 实例化RecordDao对象
RecordDao recordDao = new RecordDao();
// 添加数据到数据库
boolean b = recordDao.addRecord(record);
// 对添加操作的结果进行判断处理
if (b) {
SimpleTools.informationDialog(Alert.AlertType.INFORMATION, "提示", "信息", "添加账目成功!");
// 清空用户选择
outputRadioButton.setSelected(false);
inputRadioButton.setSelected(false);
moneyTextField.setText("");
classificationComboBox.getItems().clear();
memoTextArea.setText("");
datePickerTextField.getEditor().setText("");
} else {
SimpleTools.informationDialog(Alert.AlertType.ERROR, "提示", "错误", "添加账目失败!");
}
コード説明:取得する情報は、ユーザによって入力され、その後、決意処理の結果を追加し、追加操作にaddRecord RecordDao()を呼び出し、及び、記録エンティティ・クラス・オブジェクトをカプセル化。
:イベント処理は次のようにラジオボタンがある「支出します」
/**
* “支出”单选按钮的事件监听器
*
* @param actionEvent 事件
*/
public void outputRadioButtonEvent(ActionEvent actionEvent) {
// 获取支出分类的所有信息
List<Classification> classificationList = new ClassificationDao().selectByType("支出");
// 实例化一个一维数组
String[] classificationNames = new String[classificationList.size()];
// 将查询得到的分类名称装到一维数组中
for (int i = 0; i < classificationList.size(); i++) {
classificationNames[i] = classificationList.get(i).getcName();
}
// 给下拉列表框添加选项
publicTools.public_addComboBoxItems(classificationComboBox, classificationNames);
// 获取单选按钮项
selectedRadioButton = outputRadioButton.getText();
// 设置descriptionLabel文本内容
descriptionLabel.setText("添加" + outputRadioButton.getText());
}
すべての支出のカテゴリ名、および変換を(取得することでselectByType ClassificationDaoクラスメソッド)一次元配列に変換し、クラスPublicToolsのその後の呼び出しpublic_addComboBoxItems()メソッドのデータを記入し、物資選択したラジオボタン:定義変数に値を代入して、イベント処理「を追加」を使用。
次のようにイベント処理「収入」のラジオボタンは次のようになります。
/**
* “收入”单选按钮的事件监听器
*
* @param actionEvent 事件
*/
public void inputRadioButtonEvent(ActionEvent actionEvent) {
// 获取收入分类的所有信息
List<Classification> classificationList = new ClassificationDao().selectByType("收入");
// 实例化一个一维数组
String[] classificationNames = new String[classificationList.size()];
// 将查询得到的分类名称装到一维数组中
for (int i = 0; i < classificationList.size(); i++) {
classificationNames[i] = classificationList.get(i).getcName();
}
// 给下拉列表框添加选项
publicTools.public_addComboBoxItems(classificationComboBox, classificationNames);
// 获取单选按钮项
selectedRadioButton = inputRadioButton.getText();
//设置descriptionLabel文本内容
descriptionLabel.setText("添加" + inputRadioButton.getText());
}
「カテゴリ」ドロップダウンリストボックスのイベントハンドラは次のように:
/**
* ”分类“下拉列表框的事件监听器
*
* @param actionEvent 事件
*/
public void classificationComboBoxEvent(ActionEvent actionEvent) {
//只处理选中的状态
selectedCoboboxItem = (String) classificationComboBox.getSelectionModel().selectedItemProperty().getValue();
}
すべてのロジックのコード補完、今のプロジェクト、テスト関数を実行しています:
その後、主要なインターフェイスでの成功の追加されたレコードを表示することができます。
検索可能な公共のマイクロチャネル番号[ Javaの例のプログラム ]以上の世間の注目を得るためにFanger魏のコード番号をスキャンします。
注: [パブリック数の背景に返信20200324この章のソースコードを入手することができます]。