Java实现字典功能

package jee;

import java.awt.*;

import javax.swing.JPanel;

public class BackgroundPanel extends 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);
    }

}

package jee;

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("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("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, 150, 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("btn1.jpg")));
	}

	protected void do_searchButton_mouseExited(MouseEvent e) {
		searchButton.setIcon(new ImageIcon(MainFrame.class.getResource("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] != null && data[i][0] != null && data[i][0].equalsIgnoreCase(text)) {
	            return data[i][1];
	        }
	    }
	    return "本地没有,请尝试联网翻译";
	}


	private void readFile() {
        BufferedReader br = null;
        try {
            File dateFile = new File("E:\\aaaa杂乱\\面向对象程序设计实践-姓名-作业7(1)\\面向对象程序设计实践-姓名-作业7\\source");
            br = new BufferedReader(new InputStreamReader(new FileInputStream(dateFile)));
            String tmp = "";
            int count = 0;
            while ((tmp = br.readLine()) != null) {
                String[] a = tmp.split("#");
                // 确保每行数据有两个部分
                if (a.length >= 2) {
                    data[count][0] = a[0];
                    data[count][1] = a[1];
                    count++;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭 BufferedReader
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

package jee;

public class Sentence {
    String sentence;      // 英文句子
    String translation;   // 中文译文

    public String show(String day) {
        switch (day) {
            case "01":
                sentence = "We accept both the good and the bad, and without uttering a sound, we continue living.";
                translation = "好的坏的我们都收下吧,然后一声不响,继续生活。";
                break;
            
            case "02":
            	sentence = "We accept both the good and the bad, and without uttering a sound, we continue living.";
                translation = "好的坏的我们都收下吧,然后一声不响,继续生活。";
                break;
          
            default:
                sentence = "We accept both the good and the bad, and without uttering a sound, we continue living.";
                translation = "好的坏的我们都收下吧, 然后一声不响,继续生活。";
                break;
        }
        return sentence + "#" + translation;
    }
}

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值