用java语言设计一个中英文释义查询程序。输入英文单词,查询数据库将对应的中文显示在下框中;输入中文,查询数据库将对应的英文单词显示在下框中。
时间: 2024-02-24 16:56:36 浏览: 17
以下是一个简单的用Java语言设计的中英文释义查询程序的示例代码:
```java
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DictionaryApp extends JFrame implements ActionListener {
private JLabel wordLabel;
private JTextField wordField;
private JButton searchButton;
private JTextArea resultArea;
private Connection conn;
private PreparedStatement queryEnglishStmt;
private PreparedStatement queryChineseStmt;
public DictionaryApp() {
super("中英文释义查询程序");
// 创建界面元素
wordLabel = new JLabel("单词:");
wordField = new JTextField(20);
searchButton = new JButton("查询");
searchButton.addActionListener(this);
resultArea = new JTextArea(20, 30);
resultArea.setEditable(false);
// 创建界面布局
JPanel inputPanel = new JPanel(new FlowLayout());
inputPanel.add(wordLabel);
inputPanel.add(wordField);
inputPanel.add(searchButton);
JPanel resultPanel = new JPanel(new BorderLayout());
resultPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
resultPanel.add(new JScrollPane(resultArea));
getContentPane().setLayout(new BorderLayout());
getContentPane().add(inputPanel, BorderLayout.NORTH);
getContentPane().add(resultPanel, BorderLayout.CENTER);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/dictionary", "root", "");
queryEnglishStmt = conn.prepareStatement("SELECT chinese FROM words WHERE english = ?");
queryChineseStmt = conn.prepareStatement("SELECT english FROM words WHERE chinese = ?");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 设置窗口属性
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new DictionaryApp();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == searchButton) {
String word = wordField.getText().trim();
if (word.length() == 0) {
JOptionPane.showMessageDialog(this, "请输入单词或中文!");
return;
}
try {
if (isEnglish(word)) {
queryEnglishStmt.setString(1, word);
ResultSet rs = queryEnglishStmt.executeQuery();
if (rs.next()) {
resultArea.setText(rs.getString(1));
} else {
resultArea.setText("未找到对应的中文释义!");
}
} else {
queryChineseStmt.setString(1, word);
ResultSet rs = queryChineseStmt.executeQuery();
if (rs.next()) {
resultArea.setText(rs.getString(1));
} else {
resultArea.setText("未找到对应的英文释义!");
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
private boolean isEnglish(String word) {
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) >= '\u4e00' && word.charAt(i) <= '\u9fa5') {
return false;
}
}
return true;
}
}
```
该程序连接了一个名为dictionary的MySQL数据库,其中包含了一个名为words的表,表结构如下:
```
CREATE TABLE words (
id INT(11) NOT NULL AUTO_INCREMENT,
english VARCHAR(50) NOT NULL,
chinese VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
程序采用了简单的语言判断方式,如果用户输入的单词中包含了中文字符,则认为用户输入的是中文,否则认为用户输入的是英文。查询时会根据用户输入的语言类型,分别使用不同的SQL语句进行查询,并将查询结果显示在界面的下方文本框中。