编写上述要求java程序。
时间: 2024-02-11 15:04:23 浏览: 128
好的,以下是一个简单的Java程序,实现了您提出的要求。为了简化程序,我使用了JavaFX作为GUI框架,SQLite作为数据库,你需要先安装JavaFX和SQLite的相关库。
```java
import java.sql.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class CalendarApp extends Application {
private Connection conn;
private Statement stmt;
public void start(Stage primaryStage) {
try {
// 连接数据库
conn = DriverManager.getConnection("jdbc:sqlite:calendar.db");
stmt = conn.createStatement();
// 创建记账表
String sql = "CREATE TABLE IF NOT EXISTS account (id INTEGER PRIMARY KEY, type TEXT, cost REAL, keywords TEXT);";
stmt.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
// 创建UI界面
GridPane gridPane = new GridPane();
gridPane.setPadding(new Insets(10));
gridPane.setVgap(10);
gridPane.setHgap(10);
// 显示当月日历,当前日期,当前时间
Label monthLabel = new Label("当月日历");
gridPane.add(monthLabel, 0, 0);
// TODO: 显示当月日历
Label dateLabel = new Label("当前日期");
gridPane.add(dateLabel, 0, 1);
Label dateValueLabel = new Label();
gridPane.add(dateValueLabel, 1, 1);
// 获取当前日期
String date = new java.util.Date().toString();
dateValueLabel.setText(date);
Label timeLabel = new Label("当前时间");
gridPane.add(timeLabel, 0, 2);
Label timeValueLabel = new Label();
gridPane.add(timeValueLabel, 1, 2);
// 实时更新当前时间
Timeline timeline = new Timeline(new KeyFrame(javafx.util.Duration.seconds(1), event -> {
String time = new java.util.Date().toString().substring(11, 19);
timeValueLabel.setText(time);
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
// 查询任意月份和年份的日历
Label yearLabel = new Label("年份:");
gridPane.add(yearLabel, 0, 3);
TextField yearTextField = new TextField();
gridPane.add(yearTextField, 1, 3);
Label monthLabel2 = new Label("月份:");
gridPane.add(monthLabel2, 0, 4);
TextField monthTextField = new TextField();
gridPane.add(monthTextField, 1, 4);
Button queryButton = new Button("查询");
gridPane.add(queryButton, 2, 4);
// TODO: 查询任意月份和年份的日历
// 记账功能
Label accountLabel = new Label("记账");
gridPane.add(accountLabel, 0, 5);
Label typeLabel = new Label("类型:");
gridPane.add(typeLabel, 0, 6);
TextField typeTextField = new TextField();
gridPane.add(typeTextField, 1, 6);
Label costLabel = new Label("费用:");
gridPane.add(costLabel, 0, 7);
TextField costTextField = new TextField();
gridPane.add(costTextField, 1, 7);
Label keywordsLabel = new Label("关键字:");
gridPane.add(keywordsLabel, 0, 8);
TextField keywordsTextField = new TextField();
gridPane.add(keywordsTextField, 1, 8);
Button addButton = new Button("添加");
gridPane.add(addButton, 0, 9);
Button deleteButton = new Button("删除");
gridPane.add(deleteButton, 1, 9);
Button queryAccountButton = new Button("查询记账");
gridPane.add(queryAccountButton, 2, 9);
// 添加记账记录
addButton.setOnAction(event -> {
try {
String type = typeTextField.getText();
double cost = Double.parseDouble(costTextField.getText());
String keywords = keywordsTextField.getText();
String sql2 = "INSERT INTO account (type, cost, keywords) VALUES ('" + type + "', " + cost + ", '" + keywords + "');";
stmt.executeUpdate(sql2);
// 清空输入框
typeTextField.setText("");
costTextField.setText("");
keywordsTextField.setText("");
} catch (SQLException e) {
e.printStackTrace();
}
});
// 删除记账记录
deleteButton.setOnAction(event -> {
try {
int id = Integer.parseInt(typeTextField.getText());
String sql2 = "DELETE FROM account WHERE id=" + id + ";";
stmt.executeUpdate(sql2);
// 清空输入框
typeTextField.setText("");
} catch (SQLException e) {
e.printStackTrace();
}
});
// 查询记账记录
queryAccountButton.setOnAction(event -> {
String keywords = keywordsTextField.getText();
try {
String sql2 = "SELECT * FROM account WHERE keywords LIKE '%" + keywords + "%';";
ResultSet rs = stmt.executeQuery(sql2);
while (rs.next()) {
int id = rs.getInt("id");
String type = rs.getString("type");
double cost = rs.getDouble("cost");
String keywords2 = rs.getString("keywords");
System.out.println(id + "\t" + type + "\t" + cost + "\t" + keywords2);
}
} catch (SQLException e) {
e.printStackTrace();
}
});
// 创建场景并显示
Scene scene = new Scene(gridPane, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public void stop() {
// 关闭数据库连接
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
```
需要注意的是,这个程序只是一个简单的示例,还有很多细节需要根据您的实际需求进行调整和完善。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)