Java小程序制作——单词翻译器,原来可以这么简单

 前几天没事的时候写了一个很简陋的单词翻译器,今天来和大家分享一下。
 话不多说,先上截图:

在这里插入图片描述

接下来就来看看这个程序如何实现。
先简单整理下这个小程序中设计的几个点:

1、要熟悉Java里面的集合类Map,因为我就是用他来处理单词的英文和中文之间的对应关系。
2、要对Java Swing中的事件处理机制有一定的了解。
3、熟悉Java Swing的面板布局。

我们一步一来制作:
1、制作窗体
  这一步就比较简单了,就是运用Java的面板布局将窗口给创建出来,这里就直接贴代码了(这里只贴核心代码,在最后会给出总代码)

	private JButton button1;
	private JButton button2;
	private JTextField text;
	private JTextArea textshow;
	private Dimension Dim;					//封装窗口大小
	
	public void init()
	{
		//实例化类的数据成员
		this.Dim = new Dimension(this.getWidth() , this.getHeight());
		this.button1 = new JButton("英译汉");
		this.button2 = new JButton("汉译英");
		this.text = new JTextField();
		this.textshow = new JTextArea();
		
		//上部面板制作
		JPanel nor_pan = new JPanel();
		ImageIcon  image = new ImageIcon("src\\cn\\com\\1492060892.jpg");
		JLabel Limage = new JLabel(image);
		Limage.setPreferredSize(new Dimension(this.Dim.width, this.Dim.height/2));
		nor_pan.add(Limage);		//将图片标签添加到面板上
		this.add(nor_pan , BorderLayout.NORTH);	//上部面板nor_pan添加到容器上
		
		//中部面板制作
		JPanel cen_pan = new JPanel();
		JLabel Ltext = new JLabel("请输入要查询的内容:");
		Ltext.setFont(new Font("楷体",Font.BOLD,20));
		cen_pan.add(Ltext);								//加在面板上!!!!
		this.text.setPreferredSize(new Dimension(400 , 30));
		this.text.setHorizontalAlignment(this.text.CENTER);	//设置居中输入
		cen_pan.add(this.text);		//文本框添加到面板
		this.textshow.setPreferredSize(new Dimension(600 , 150));
		cen_pan.add(this.textshow);
		this.add(cen_pan , BorderLayout.CENTER);			//中间面板cen_pan添加到容器上
		
		
		//底部面板制作
		FlowLayout flowlayout = new FlowLayout();
		flowlayout.setHgap(80); 	//组件之间设置80的水平间隔
		JPanel sou_pan = new JPanel();
		sou_pan.setLayout(flowlayout);
		this.button1.setPreferredSize(new Dimension(100,30));
		this.button2.setPreferredSize(new Dimension(100,30));
		sou_pan.add(this.button1);
		sou_pan.add(this.button2);
		this.add(sou_pan , BorderLayout.SOUTH);
			
		
		//窗口属性设置
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("我的单词查阅器");
		this.setVisible(true);
		
	}
	

2、事件处理机制
  这里我是按两个事件处理的。

  • 第一个是窗口事件。当窗口为当前活动窗口时,就在程序中添加单词,作为后面查找单词的预备库。
  • 第二个事件自然是按钮事件了。分别单击两个按钮时,窗口做出相应的反应。

这里先给出窗口事件的代码,比较简单。

//窗口激活监听实例类
	private class WindowHandler extends WindowAdapter
	{
		//窗口激活时添加储备单词
		public void windowActivated(WindowEvent e)
		{
			DictionaryDemo.this.map = new HashMap<String , String>();
			DictionaryDemo.this.map.put("apple" , "苹果");
			DictionaryDemo.this.map.put("pear" , "梨");
			DictionaryDemo.this.map.put("pig" , "猪");
			DictionaryDemo.this.map.put("animal" , "动物");
			DictionaryDemo.this.map.put("book" , "书本");		
		}

		
	}

 下面在给出按钮事件之前,我们先来分析一下。
  我们知道,Map集合存储的数据有对应关系(Map<K , V >),他有一个get()方法用来寻找key对应的Value值。我们这里可以对应英译汉。那么怎样实现汉译英呢?
 因为Map集合里面很容易由key去寻找value(因为key不允许重复,但是value可以重复)。所以我们应该先来写一个函数让我们可以通过value的值去寻找key

这里不考虑多个value对应key,就算有,我们也是取第一个value对应的key

 public  String getKeyByValue(Map map, Object value) 
	    {
	        // 首先得到entrySet的迭代器it
	        Iterator it =  map.entrySet().iterator();
	        Map.Entry entry = null;
	        boolean match = false;
	        while (it.hasNext()) 
	        {
	            entry = (Map.Entry) it.next();
	            Object obj = entry.getValue();
	            if (value.equals(obj)) 
	            {
	            	match = true;
	            	break;
	            }
	        }
	        if (match == true)
	        	return (String) entry.getKey();
	        else
	        	return " ";
	    }

  下面就可以写按钮事件了。

//按钮事件监听实例类
	private class myActionEvent implements ActionListener
	{
		private int Statue;		//状态参数
		//设置是汉译英还是英译汉
		public myActionEvent(int Statue)
		{
			this.Statue = Statue;
		}
		
		public void actionPerformed(ActionEvent e)
		{
			String str = DictionaryDemo.this.text.getText();	//获取用户的输入文本
			
			//若果用户输入为空
			if (str==null || str.isEmpty())
			{
				JOptionPane.showMessageDialog(DictionaryDemo.this, "请输入要查询的单词" , null , JOptionPane.WARNING_MESSAGE);
				return ;
			}
				
			//开始查找单词
			if (this.Statue==1)
			{
				String meaning1 = DictionaryDemo.this.map.get(str);
				if (meaning1 == null)
				{
					JOptionPane.showMessageDialog(DictionaryDemo.this, "没有查到您输入的单词" , null , JOptionPane.WARNING_MESSAGE);
					return;
				}
					
				else
					DictionaryDemo.this.textshow.setText(meaning1);
			}
			else if(this.Statue==2)
			{
				String meaning2 =getKeyByValue(DictionaryDemo.this.map , str);
				if (meaning2 == " ")
				{
					JOptionPane.showMessageDialog(DictionaryDemo.this, "没有查到您输入的单词" , null , JOptionPane.WARNING_MESSAGE);
					return;
				}
					
				else
					DictionaryDemo.this.textshow.setText((String) meaning2);
			}
			
		}

下面给出总代码:

package cn.com;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class DictionaryDemo  extends JFrame
{
	private JButton button1;
	private JButton button2;
	private JTextField text;
	private JTextArea textshow;
	private Map<String , String> map;		//单词和中文意思 的映射
	private Dimension Dim;					//封装窗口大小
	
	public void init()
	{
		//实例化类的数据成员
		this.Dim = new Dimension(this.getWidth() , this.getHeight());
		this.button1 = new JButton("英译汉");
		this.button2 = new JButton("汉译英");
		this.text = new JTextField();
		this.textshow = new JTextArea();
		
		//上部面板制作
		JPanel nor_pan = new JPanel();
		ImageIcon  image = new ImageIcon("src\\cn\\com\\1492060892.jpg");
		JLabel Limage = new JLabel(image);
		Limage.setPreferredSize(new Dimension(this.Dim.width, this.Dim.height/2));
		nor_pan.add(Limage);		//将图片标签添加到面板上
		this.add(nor_pan , BorderLayout.NORTH);	//上部面板nor_pan添加到容器上
		
		//中部面板制作
		JPanel cen_pan = new JPanel();
		JLabel Ltext = new JLabel("请输入要查询的内容:");
		Ltext.setFont(new Font("楷体",Font.BOLD,20));
		cen_pan.add(Ltext);								//加在面板上!!!!
		this.text.setPreferredSize(new Dimension(400 , 30));
		this.text.setHorizontalAlignment(this.text.CENTER);	//设置居中输入
		cen_pan.add(this.text);		//文本框添加到面板
		this.textshow.setPreferredSize(new Dimension(600 , 150));
		cen_pan.add(this.textshow);
		this.add(cen_pan , BorderLayout.CENTER);			//中间面板cen_pan添加到容器上
		
		
		//底部面板制作
		FlowLayout flowlayout = new FlowLayout();
		flowlayout.setHgap(80); 	//组件之间设置80的水平间隔
		JPanel sou_pan = new JPanel();
		sou_pan.setLayout(flowlayout);
		this.button1.setPreferredSize(new Dimension(100,30));
		this.button2.setPreferredSize(new Dimension(100,30));
		sou_pan.add(this.button1);
		sou_pan.add(this.button2);
		this.add(sou_pan , BorderLayout.SOUTH);
		
		//注册监视器
		this.button1.addActionListener(new myActionEvent(1));
		this.button2.addActionListener(new myActionEvent(2));
		this.addWindowListener(new WindowHandler());
		
		
		//窗口属性设置
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("我的单词查阅器");
		this.setVisible(true);
		
	}
	
	//窗口激活监听实例类
	private class WindowHandler extends WindowAdapter
	{
		//窗口激活时添加储备单词
		public void windowActivated(WindowEvent e)
		{
			DictionaryDemo.this.map = new HashMap<String , String>();
			DictionaryDemo.this.map.put("apple" , "苹果");
			DictionaryDemo.this.map.put("pear" , "梨");
			DictionaryDemo.this.map.put("pig" , "猪");
			DictionaryDemo.this.map.put("animal" , "动物");
			DictionaryDemo.this.map.put("book" , "书本");		
		}

		
	}
	
	//按钮事件监听实例类
	private class myActionEvent implements ActionListener
	{
		private int Statue;		//状态参数
		//设置是汉译英还是英译汉
		public myActionEvent(int Statue)
		{
			this.Statue = Statue;
		}
		
		public void actionPerformed(ActionEvent e)
		{
			String str = DictionaryDemo.this.text.getText();	//获取用户的输入文本
			
			//若果用户输入为空
			if (str==null || str.isEmpty())
			{
				JOptionPane.showMessageDialog(DictionaryDemo.this, "请输入要查询的单词" , null , JOptionPane.WARNING_MESSAGE);
				return ;
			}
				
			//开始查找单词
			if (this.Statue==1)
			{
				String meaning1 = DictionaryDemo.this.map.get(str);
				if (meaning1 == null)
				{
					JOptionPane.showMessageDialog(DictionaryDemo.this, "没有查到您输入的单词" , null , JOptionPane.WARNING_MESSAGE);
					return;
				}
					
				else
					DictionaryDemo.this.textshow.setText(meaning1);
			}
			else if(this.Statue==2)
			{
				String meaning2 =getKeyByValue(DictionaryDemo.this.map , str);
				if (meaning2 == " ")
				{
					JOptionPane.showMessageDialog(DictionaryDemo.this, "没有查到您输入的单词" , null , JOptionPane.WARNING_MESSAGE);
					return;
				}
					
				else
					DictionaryDemo.this.textshow.setText((String) meaning2);
			}
			
		}	
		
		// 通过map的value得到key
	    public  String getKeyByValue(Map map, Object value) 
	    {
	        // 首先得到entrySet的迭代器it
	        Iterator it =  map.entrySet().iterator();
	        Map.Entry entry = null;
	        boolean match = false;
	        while (it.hasNext()) 
	        {
	            entry = (Map.Entry) it.next();
	            Object obj = entry.getValue();
	            if (value.equals(obj)) 
	            {
	            	match = true;
	            	break;
	            }
	        }
	        if (match == true)
	        	return (String) entry.getKey();
	        else
	        	return " ";
	    }
		
	}
	

	
	public DictionaryDemo()
	{
		this.setSize(700, 500);
		init();
	}
	
	
	

}

测试

package cn.com;

public class Test 
{
	public static void main(String args[])
	{
		DictionaryDemo win = new DictionaryDemo();
	
	}
}

以上就是我们这个程序的全部内容了,由于很简单,所以这里也就没有过多详细的去讲解,如果有疑问的话可以直接私聊我~~~。

  • 6
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值