详细解释都在注释当中,请注意,背景图片和组件图片路径需要修改,还需要一个命名为source的库文件作为查找源,话不多说上代码
import javax.print.attribute.standard.JobMessageFromOperator;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Date;
public class MainFrame extends JFrame {
private JPanel contentPane; // 内容面板
private JTextField textField; // 文本框
private JButton searchButton; // 搜索按钮
private JLabel noteLabel; // “基础释义”标签
private JLabel wordLabel; // “单词”标签
private JLabel explainLabel; // “词义”标签
private String[][] data=new String[7989][2]; // 存储单词和词义的二维数组
private JLabel dateLabel; // “日期”标签
private JLabel sentenceLabel; // “英文句子”标签
private JLabel lblTranslation; // “中文译文”标签
public static void main(String[] args){
MainFrame frame = new MainFrame(); // 创建主窗体
frame.setVisible(true); // 使主窗体可见
}
public MainFrame() {
setTitle("单词英译汉"); // 设置主窗体的标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置主窗体的关闭方式
setBounds(340, 150, 809, 500); // 设置主窗体的位置与宽高
contentPane = new JPanel(); // 创建内容面板
contentPane.setLayout(new BorderLayout(0, 0)); // 设置内容面板的布局为边
setContentPane(contentPane); // 把内容面板放置在主窗体中
BackgroundPanel backgroundPanel = new BackgroundPanel(); // 创建背景面板
backgroundPanel.setImage(getToolkit().getImage(getClass().getResource("/pic/main.png"))); // 设置背景图片
contentPane.add(backgroundPanel, BorderLayout.CENTER); // 把背景面板添加到内容面板中
textField = new JTextField(); // 创建文本框
textField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
do_textField_removeUpdate(e);
}
@Override
public void insertUpdate(DocumentEvent e) {
}
@Override
public void changedUpdate(DocumentEvent e) {
}
});
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == '\n')
searchButton.doClick();
}
});
textField.setBounds(133, 82, 453, 50); // 设置文本框的位置与宽高
backgroundPanel.add(textField); // 把文本框添加到背景面板中
searchButton = new JButton(); // 创建搜索按钮
searchButton.addMouseListener(new MouseListener() {
@Override
public void mouseEntered(MouseEvent e) {
do_searchButton_mouseEntered(e);
}
@Override
public void mouseExited(MouseEvent e) {
do_searchButton_mouseExited(e);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mousePressed(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO 自动生成的方法存根
}
});
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
do_searchButton_actionPerfromed(e);
}
});
searchButton.setIcon(new ImageIcon(MainFrame.class.getResource("/pic/btn1.jpg"))); // 设置搜索按钮的图标
searchButton.setBounds(585, 82, 52, 49); // 设置搜索按钮的位置与宽高
backgroundPanel.add(searchButton); // 把搜索按钮添加到背景面板中
noteLabel = new JLabel(); // 创建“基础释义”标签
noteLabel.setForeground(new Color(51, 153, 204)); // 设置“基础释义”标签的前景色
noteLabel.setFont(new Font("黑体", Font.PLAIN, 16)); // 设置“基础释义”标签的字体样式
noteLabel.setBounds(133, 147, 100, 30); // 设置“基础释义”标签的位置与宽高
backgroundPanel.add(noteLabel); // 把“基础释义”标签添加到背景面板中
wordLabel = new JLabel(); // 创建“单词”标签
wordLabel.setFont(new Font("黑体", Font.BOLD, 20)); // 设置“单词”标签的字体样式
wordLabel.setBounds(133, 180, 200, 30); // 设置“单词”标签的位置与宽高
backgroundPanel.add(wordLabel); // 把“单词”标签添加到背景面板中
explainLabel = new JLabel(); // 创建“词义”标签
explainLabel.setFont(new Font("宋体", Font.PLAIN, 20)); // 设置“词义”标签的字体样式
explainLabel.setBounds(133, 220, 300, 30); // 设置“词义”标签的位置与宽高
backgroundPanel.add(explainLabel); // 把“词义”标签添加到背景面板中
dateLabel = new JLabel(); // 创建“日期”标签
dateLabel.setForeground(new Color(51,153,204)); // 设置“日期”标签的字体颜色
dateLabel.setFont(new Font("黑体", Font.BOLD, 18)); // 设置“日期”标签的字体样式
Date date = new Date(); // 创建Date类对象
String strDate = date + ""; // 把Date类对象转换为String类型
System.out.println(strDate);
int spaceFirst = strDate.indexOf(" "); // 获得第一个空格的索引
int spaceSecond = strDate.indexOf(" ", spaceFirst + 1); // 获得第二个空格的索引
int spaceThird = strDate.indexOf(" ", spaceSecond + 1); // 获得第三个空格的索引
String target = strDate.substring(spaceFirst + 1, spaceThird); // 获取月份和日期
dateLabel.setText(target); // 把“日期”标签的文本内容设置为已获得的月份和日期
dateLabel.setBounds(133, 260, 30, 30); // 设置“日期”标签的位置与宽高
backgroundPanel.add(dateLabel); // 把“日期”标签添加到背景面板中
String day = strDate.substring(spaceSecond + 1, spaceThird); // 获取日期
Sentence sentence = new Sentence(); // 创建语句类对象
String strSentence = sentence.show(day); // 根据已获得的日期,获取“英文句子”和“中文译文”
String[] str = strSentence.split("#");
sentenceLabel = new JLabel(); // 创建“英文句子“标签
sentenceLabel.setFont(new Font("Times New Roman", Font.ITALIC, 14));
sentenceLabel.setText("<html>" + str[0] + "</html>"); //“英文子签显“
sentenceLabel.setBounds(133, 295, 504, 40); // 置“英文子”标签的位置与宽高
backgroundPanel.add(sentenceLabel); // 把“英文子”标签添加到背景面板中
lblTranslation = new JLabel(); // 创建“中文译文”标签
lblTranslation.setFont(new Font("宋体", Font.PLAIN, 14)); // 设置“文议”
lblTranslation.setText("<html>" + str[1] + "</html>"); //“中文”标签显“
lblTranslation.setBounds(133, 340, 504, 40); // 设置“中文译文”标签的位置与宽高
backgroundPanel.add(lblTranslation); // 把“中文译文”标签加到背景面板中
}
// 鼠标进入搜索按钮的操作
protected void do_searchButton_mouseEntered(MouseEvent e){
searchButton.setIcon(new ImageIcon(MainFrame.class.getResource("/pic/btn1.jpg"))); // 设置搜索按钮的图标为高亮状态
}
// 鼠标离开搜索按钮的操作
protected void do_searchButton_mouseExited(MouseEvent e) {
searchButton.setIcon(new ImageIcon(MainFrame.class.getResource("/pic/btn0.jpg"))); // 设置搜索按钮的图标为普通状态
}
// 搜索按钮点击事件的操作
protected void do_searchButton_actionPerfromed(ActionEvent e) {
String word=textField.getText(); // 获取文本框中的单词
if(word.equals("")){ // 如果输入为空
JOptionPane.showMessageDialog(null,"请输入要查询的单词","警告",JOptionPane.CLOSED_OPTION); // 弹出警告对话框,提示用户输入单词
} else if (!word.matches(("^[A-Za-z]+$"))) { // 如果输入的单词不符合英文字符的格式
JOptionPane.showMessageDialog(null,"注意","只能输入英文字符(大小写不限)",JOptionPane.CLOSED_OPTION); // 弹出警告对话框,提示用户输入正确的格式
}else {
readFile(); // 读取单词和词义的数据文件
translation(); // 进行翻译操作
}
}
// 文本框内容被删除时的操作
protected void do_textField_removeUpdate(DocumentEvent e){
noteLabel.setText(""); // 清空“基础释义”标签的文本内容
wordLabel.setText(""); // 清空“单词”标签的文本内容
explainLabel.setText(""); // 清空“词义”标签的文本内容
}
// 进行翻译的操作
private void translation(){
String text=textField.getText().trim(); // 获取去除首尾空格的文本框内容
String trans=getTrans(text); // 根据文本框内容获取翻译结果
if("".equalsIgnoreCase(trans)|| null == trans){ // 如果翻译结果为空
explainLabel.setText("本地没有,请尝试联网翻译"); // 在“词义”标签中显示提示信息
}else {
noteLabel.setText("基础释义"); // 设置“基础释义”标签的文本内容
wordLabel.setText(textField.getText().toLowerCase()); // 把文本框的内容设置为小写,并在“单词”标签中显示
explainLabel.setText(trans); // 在“词义”标签中显示翻译结果
}
}
// 根据单词获取翻译结果的方法
public String getTrans(String text) {
for (int i = 0; i< data.length; i++){ // 遍历存储单词和词义的二维数组
if(data[i][0].equalsIgnoreCase(text)){ // 如果找到了匹配的单词
return data[i][1]; // 返回对应的词义
}
}
return"本地木有,请尝试联同窗译"; // 如果未找到匹配的单词,则返回提示信息
}
// 读取单词和词义的数据文件的方法
private void readFile() {
BufferedReader br = null;
try {
File dateFile = new File("src/word/source"); // 创建文件对象,指定数据文件的路径
br = new BufferedReader(new InputStreamReader(new FileInputStream(dateFile))); // 创建BufferedReader对象,读取数据文件的内容
String tmp="";
for (int count = 0;(tmp = br.readLine()) != null; count++) { // 按行读取数据文件的内容
String[] a= tmp.split("#"); // 使用“#”分隔每行的单词和词义
data[count][0]=a[0]; // 把单词存储到二维数组中的第一列
data[count][1]=a[1]; // 把词义存储到二维数组中的第二列
}
}catch (FileNotFoundException e){
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
}
2
import java.awt.*;
import javax.swing.JPanel;
public class BackgroundPanel extends JPanel{ // 继承自JPanel类
private Image image; // 图像成员变量
public BackgroundPanel(){ // 构造方法
super(); // 调用父类的构造方法
setOpaque(false); // 设置透明
setLayout(null); // 不设置布局管理器
}
public void setImage(Image image){ // 设置图像的方法
this.image=image; // 把传入的图像赋值给成员变量
}
protected void paintComponent(Graphics g){ // 绘制组件的方法
if(image!=null){ // 如果图像不为空
int width=getWidth(); // 获取组件的宽度
int height=getHeight(); // 获取组件的高度
g.drawImage(image,0,0,width,height,this); // 绘制图像
}
super.paintComponent(g); // 调用父类的绘制方法
}
}
3.
public class Sentence{
String sentence;//英文句子
String translation;//中文译文
public String show(String day) { //显示“英文句子》和“中文译文”的方法,参数力String
switch(day){ //利用String型的日期与case后的值作比较
case "01":// String型的日期为"01"
// 为成员变量“英文句子》和“中文译文”赋值
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;//跳出String型的日期为“01"的代码块
case "02":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "03":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "04":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "05":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "06":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "07":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "08":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "09":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "10":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "11":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "12":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "13":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "14":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "15":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "16":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "17":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "18":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "19":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "20":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "21":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "22":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "23":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "24":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "25":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "26":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "27":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "28":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "29":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "30":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
case "31":
sentence = "Whatever is worth doing is worth doing well.";
translation ="任何值得做的,就把它做好";
break;
}
return sentence+"#"+translation;
}
}