设计 GUI 界面的单词簿。具体要求如下: 1)使用内置 Derby 数据库。在数据库中使用表存储单词和该单词的翻译解 释,例如“sun,太阳”“moon,月亮”等。 2)通过 GUI 界面管理单词簿。可以向单词簿添加单词,可以修改单词簿中 的单词,可以删除单词簿中的单词。 3)通过 GUI 界面查询单词。可以查询一个,随机查询若干个或全部单词。
时间: 2024-03-05 11:54:12 浏览: 81
好的,根据您的要求,以下是一个简单的单词簿设计:
1. 数据设计
使用 Derby 数据库,在其中创建一个表格 `words`,包含两个字段:`word` 和 `translation`,分别用于存储单词和翻译。
```
CREATE TABLE words (
word VARCHAR(50) PRIMARY KEY,
translation VARCHAR(100)
);
```
2. GUI 界面设计
GUI 界面可以使用 Java Swing 实现。可以使用以下组件:
- JFrame:主窗口
- JLabel:用于显示提示信息或查询结果
- JTextField:用于输入单词或查询条件
- JButton:用于触发添加、修改、删除、查询操作
GUI 界面应该包含以下功能:
- 添加单词:输入单词和翻译,点击“添加”按钮将其添加到数据库中。
- 修改单词:输入要修改的单词和新的翻译,点击“修改”按钮将其更新到数据库中。
- 删除单词:输入要删除的单词,点击“删除”按钮将其从数据库中删除。
- 查询单词:输入查询条件,可以查询一个、随机查询若干个或查询全部单词。
3. Java 代码实现
下面是一个简单的 Java 代码实现:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Random;
public class WordBookGUI {
private JFrame frame;
private JLabel messageLabel;
private JTextField wordField;
private JTextField translationField;
private JTextField searchField;
private Connection conn;
public WordBookGUI() {
// 初始化 GUI 界面
frame = new JFrame("单词簿");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLayout(new GridLayout(5, 2));
// 添加单词
JLabel wordLabel = new JLabel("单词:");
wordField = new JTextField();
JLabel translationLabel = new JLabel("翻译:");
translationField = new JTextField();
JButton addButton = new JButton("添加");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addWord();
}
});
// 修改单词
JLabel wordLabel2 = new JLabel("单词:");
JTextField wordField2 = new JTextField();
JLabel translationLabel2 = new JLabel("翻译:");
JTextField translationField2 = new JTextField();
JButton updateButton = new JButton("修改");
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateWord(wordField2.getText(), translationField2.getText());
}
});
// 删除单词
JLabel wordLabel3 = new JLabel("单词:");
JTextField wordField3 = new JTextField();
JButton deleteButton = new JButton("删除");
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteWord(wordField3.getText());
}
});
// 查询单词
JLabel searchLabel = new JLabel("查询条件:");
searchField = new JTextField();
JButton searchButton = new JButton("查询");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchWord(searchField.getText());
}
});
JButton randomButton = new JButton("随机查询");
randomButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
randomSearch();
}
});
JButton allButton = new JButton("查看全部");
allButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
viewAll();
}
});
// 提示信息
messageLabel = new JLabel("请选择操作");
// 添加组件到窗口中
frame.add(wordLabel);
frame.add(wordField);
frame.add(translationLabel);
frame.add(translationField);
frame.add(addButton);
frame.add(new JLabel());
frame.add(wordLabel2);
frame.add(wordField2);
frame.add(translationLabel2);
frame.add(translationField2);
frame.add(updateButton);
frame.add(new JLabel());
frame.add(wordLabel3);
frame.add(wordField3);
frame.add(deleteButton);
frame.add(new JLabel());
frame.add(searchLabel);
frame.add(searchField);
frame.add(searchButton);
frame.add(randomButton);
frame.add(allButton);
frame.add(messageLabel);
// 连接数据库
try {
conn = DriverManager.getConnection("jdbc:derby:wordbook;create=true");
Statement stmt = conn.createStatement();
stmt.executeUpdate("CREATE TABLE words (word VARCHAR(50) PRIMARY KEY, translation VARCHAR(100))");
stmt.close();
} catch (SQLException ex) {
if (ex.getSQLState().equals("X0Y32")) {
// 表已经存在,不需要创建
} else {
ex.printStackTrace();
}
}
// 显示窗口
frame.setVisible(true);
}
private void addWord() {
String word = wordField.getText();
String translation = translationField.getText();
if (word.isEmpty() || translation.isEmpty()) {
messageLabel.setText("单词和翻译不能为空");
return;
}
try {
Statement stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO words (word, translation) VALUES ('" + word + "', '" + translation + "')");
stmt.close();
messageLabel.setText("添加成功");
} catch (SQLException ex) {
if (ex.getSQLState().equals("23505")) {
messageLabel.setText("单词已存在");
} else {
ex.printStackTrace();
}
}
}
private void updateWord(String oldWord, String newTranslation) {
if (oldWord.isEmpty() || newTranslation.isEmpty()) {
messageLabel.setText("单词和翻译不能为空");
return;
}
try {
Statement stmt = conn.createStatement();
stmt.executeUpdate("UPDATE words SET translation='" + newTranslation + "' WHERE word='" + oldWord + "'");
stmt.close();
messageLabel.setText("修改成功");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void deleteWord(String word) {
if (word.isEmpty()) {
messageLabel.setText("单词不能为空");
return;
}
try {
Statement stmt = conn.createStatement();
stmt.executeUpdate("DELETE FROM words WHERE word='" + word + "'");
stmt.close();
messageLabel.setText("删除成功");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void searchWord(String condition) {
String sql;
if (condition.isEmpty()) {
sql = "SELECT * FROM words";
} else {
sql = "SELECT * FROM words WHERE word LIKE '%" + condition + "%' OR translation LIKE '%" + condition + "%'";
}
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ArrayList<String> results = new ArrayList<String>();
while (rs.next()) {
String word = rs.getString("word");
String translation = rs.getString("translation");
results.add(word + "," + translation);
}
rs.close();
stmt.close();
if (results.isEmpty()) {
messageLabel.setText("没有查询到结果");
} else {
messageLabel.setText("查询结果:\n" + String.join("\n", results));
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void randomSearch() {
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM words");
rs.next();
int count = rs.getInt(1);
rs.close();
stmt.close();
Random random = new Random();
int n = random.nextInt(count) + 1;
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM words OFFSET " + n + " ROWS FETCH NEXT 10 ROWS ONLY");
ArrayList<String> results = new ArrayList<String>();
while (rs.next()) {
String word = rs.getString("word");
String translation = rs.getString("translation");
results.add(word + "," + translation);
}
rs.close();
stmt.close();
if (results.isEmpty()) {
messageLabel.setText("没有查询到结果");
} else {
messageLabel.setText("查询结果:\n" + String.join("\n", results));
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void viewAll() {
searchWord("");
}
public static void main(String[] args) {
new WordBookGUI();
}
}
```
注意,这只是一个简单的实现,还有很多细节需要考虑。例如,对输入的合法性进行检查,防止 SQL 注入攻击等。
阅读全文