⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex.java

📁 一个用JAVA程序编写的发音电子词典编译器
💻 JAVA
字号:
import java.awt.*;
import java.net.*;
import java.sql.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.io.*;
import sun.audio.*;

class Ex extends Frame implements ActionListener
{
	MenuBar menubar=new MenuBar();//菜单
	Menu fileMenu,editMenu,helpMenu;
	MenuItem fileenglish,filechinese,exit,editAdd,editmod,editDel;
	TextField inputtext;
	TextArea txt;
	Label label1,label2;
	Button btn1,btnsound;
	Panel p,p1,p2,p3;
	
	Ex()
	{
		super("电子词典");
		setBounds(200,200,350,300);
		setMenuBar(menubar);
		
		fileMenu=new Menu("文件");
		editMenu=new Menu("编辑");
		helpMenu=new Menu("帮助");
		
		fileenglish=new MenuItem("英汉词典");
		filechinese=new MenuItem("汉英词典");
		exit=new MenuItem("退出");
		editAdd=new MenuItem("添加词汇");
		editmod=new MenuItem("修改词汇");
		editDel=new MenuItem("删除词汇");
		
		menubar.add(fileMenu);
		menubar.add(editMenu);
		menubar.add(helpMenu);
		
		fileMenu.add(fileenglish);
		fileMenu.add(filechinese);
		fileMenu.addSeparator();
		fileMenu.add(exit);
		editMenu.add(editAdd);
		editMenu.add(editmod);
		editMenu.add(editDel);
		
		inputtext=new TextField("",10);
		txt=new TextArea(10,10);
		label1=new Label("输入要查询的英语单词:");
		label2=new Label("查询结果:");
		btn1=new Button("查询");
		btnsound=new Button("发音");
		
		p=new Panel(new BorderLayout());
		p2=new Panel(new FlowLayout(FlowLayout.LEFT,5,0));
		
		p2.add(label1);
		p2.add(inputtext);
		p2.add(btn1);
		p2.add(btnsound);
		add(p2,"North");
		p.add(label2,"North");
		p.add(txt,"Center");
		add(p,"Center");
		
		setVisible(true);
		setResizable(false);
		validate();
		
		fileenglish.addActionListener(this);
		filechinese.addActionListener(this);
		exit.addActionListener(this);
		editAdd.addActionListener(this);
		editmod.addActionListener(this);
		editDel.addActionListener(this);
		btn1.addActionListener(this);
		btnsound.addActionListener(this);
		
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==fileenglish)//英汉(外观变化)
		{
			label1.setText("输入要查询的英语单词:");
			label2.setText("查询结果:");
			txt.setText("");
			btn1.setLabel("查询");
			btnsound.setVisible(true);
		}
		else if(e.getSource()==filechinese)//汉英(外观变化)
		{
			label1.setText("输入要查询的汉语词语:");
			label2.setText("查询结果:");
			txt.setText("");
			btn1.setLabel("查询");
			btnsound.setVisible(true);
		}
		else if(e.getSource()==exit)//退出
		{
			System.exit(0);
		}
		else if(e.getSource()==btn1)
		{
			if(btn1.getLabel().equals("查询"))//实现查询功能(包括英汉或汉英)
			{
				txt.setText(null);
				try
				{
					Listwords();
				}
				catch(SQLException ee){}
			}
			else if(btn1.getLabel().equals("提交"))//实现添加功能
			{
				try
				{
					addwords();
				}
				catch(SQLException ee){}
			}
			else if(btn1.getLabel().equals("更新"))//实现修改功能
			{
				try
				{
					modwords();
				}
				catch(SQLException ee){}
			}
			else if(btn1.getLabel().equals("删除"))//实现删除功能
			{
				try
				{
					delwords();
				}
				catch(SQLException ee){}
			}
		}
		else if(e.getSource()==editAdd)//添加(外观变化)
		{
			label1.setText("输入新单词:");
			label2.setText("输入中文解释:");
			btn1.setLabel("提交");
			btnsound.setVisible(false);
		}
		else if(e.getSource()==editmod)//修改(外观变化)
		{
			label1.setText("输入要修改的单词:");
			label2.setText("输入更新后的解释:");
			btn1.setLabel("更新");
			btnsound.setVisible(false);
		}
		else if(e.getSource()==editDel)//删除(外观变化)
		{
			label1.setText("输入要删除的单词:");
			label2.setText("");
			btn1.setLabel("删除");
			btnsound.setVisible(false);
		}
		else if(e.getSource()==btnsound)//发音
		{
			if(inputtext.getText()!=null)
			{
				try
         		{
       
		             InputStream is=getClass().getResource("sound//"+
		             	inputtext.getText().trim()+".wav").openStream();
		             AudioPlayer.player.start(is);
	             
		         }
		         catch(IOException e1){}
			}
		}
	}
	
	public void Listwords() throws SQLException//查询实现过程
	{
		String cname,ename;
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}
		catch(ClassNotFoundException e){}
		
		Connection Ex1Con=DriverManager.getConnection("jdbc:odbc:redsun","","");
		Statement Ex1Stmt=Ex1Con.createStatement();
		ResultSet rs=Ex1Stmt.executeQuery("SELECT * FROM words");
		boolean boo=false;
		while((boo=rs.next())==true)
		{
			ename=rs.getString("英语");
			cname=rs.getString("汉语");
			if(ename.equals(inputtext.getText())&&
			label1.getText().equals("输入要查询的英语单词:"))
			{
				txt.append(cname);
				break;
			}
			else if(cname.equals(inputtext.getText())&&
			label1.getText().equals("输入要查询的汉语词语:"))
			{
				txt.append(ename);
				break;
			}
		}
		Ex1Con.close();
		if(boo==false)
		{
			JOptionPane.showMessageDialog(this,"查无此单词!","警告",
			JOptionPane.WARNING_MESSAGE);
		}
	}
	
	public void addwords() throws SQLException//向数据库添加新词汇
	{
		String cname,ename;
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}
		catch(ClassNotFoundException e){}
		
		Connection Ex1Con=DriverManager.getConnection("jdbc:odbc:redsun","","");
		Statement Ex1Stmt=Ex1Con.createStatement();
		ResultSet rs=Ex1Stmt.executeQuery("SELECT * FROM words");
		boolean boo=false;
		while((boo=rs.next())==true)
		{
			ename=rs.getString("英语");
			cname=rs.getString("汉语");
			if(ename.equals(inputtext.getText())&&cname.equals(txt.getText()))
			{
				JOptionPane.showMessageDialog(this,"此词汇已存在!","警告",
				JOptionPane.WARNING_MESSAGE);
				break;
			}
		}
		if(boo==false)
		{
			Ex1Stmt.executeUpdate("INSERT INTO words (英语,汉语) VALUES ('"+
			inputtext.getText().trim()+"','"+txt.getText().trim()+"')");
			JOptionPane.showMessageDialog(this,"添加成功!","恭喜",
				JOptionPane.WARNING_MESSAGE);
		}
		Ex1Con.close();	
	}
	
	public void modwords() throws SQLException//修改词库中记录
	{
		String cname,ename;
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}
		catch(ClassNotFoundException e){}
		
		Connection Ex1Con=DriverManager.getConnection("jdbc:odbc:redsun","","");
		Statement Ex1Stmt=Ex1Con.createStatement();
		ResultSet rs=Ex1Stmt.executeQuery("SELECT * FROM words");
		boolean boo=false;
		while((boo=rs.next())==true)
		{
			ename=rs.getString("英语");
			cname=rs.getString("汉语");
			if(ename.equals(inputtext.getText()))
			{
				Ex1Stmt.executeUpdate("UPDATE words SET 汉语='"+txt.getText().trim()
				+"' WHERE 英语='"+inputtext.getText().trim()+"'");
				JOptionPane.showMessageDialog(this,"记录修改成功!","恭喜",
				JOptionPane.WARNING_MESSAGE);
				break;
			}
		}
		Ex1Con.close();	
		if(boo==false)
		{
			JOptionPane.showMessageDialog(this,"不存在此单词!","警告",
			JOptionPane.WARNING_MESSAGE);
		}
	}
	
	public void delwords() throws SQLException//删除词库中记录
	{
		String cname,ename;
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}
		catch(ClassNotFoundException e){}
		
		Connection Ex1Con=DriverManager.getConnection("jdbc:odbc:redsun","","");
		Statement Ex1Stmt=Ex1Con.createStatement();
		ResultSet rs=Ex1Stmt.executeQuery("SELECT * FROM words");
		boolean boo=false;
		while((boo=rs.next())==true)
		{
			ename=rs.getString("英语");
			cname=rs.getString("汉语");
			if(ename.equals(inputtext.getText()))
			{
				Ex1Stmt.executeUpdate("DELETE FROM words WHERE 英语='"+
				inputtext.getText().trim()+"'");
				JOptionPane.showMessageDialog(this,"成功删除记录!","恭喜",
				JOptionPane.WARNING_MESSAGE);
				break;
			}
		}
		Ex1Con.close();	
		if(boo==false)
		{
			JOptionPane.showMessageDialog(this,"不存在此单词!","警告",
			JOptionPane.WARNING_MESSAGE);
		}	
	}
	
	public static void main(String args[])
	{
		new Ex();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -