资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

中英字典源代码java,字典的代码

一个Java编写的英汉词典

说先说:你的代码有错误,在最下面,我已经该过了,不是大问题。

创新互联是一家集网站建设,金平企业网站建设,金平品牌网站建设,网站定制,金平网站建设报价,网络营销,网络优化,金平网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

如果你想学好java的话,可以根据这个代码加上我的注解去理解,但不要学这个代码的变成方式或者说变成习惯,这个代码基本上无误,但犯了一些常识性问题,如果养成了这些不好的习惯对以后的编程会有坏的影响。

我是英文学的java,所以有些注解可能不通顺,但我尽力而为了。 有的注解有点长,所以你复制到编译器后稍微编辑一下就可以运行。代码是可以运行,没有问题的。

------------------------------------

import javax.swing.*; //用来创建图形界面,如窗口,表格,按钮等。

import java.awt.*; //作用同上,但已经很少用,能用swing的地方就不要用awt

import java.awt.event.*;//事件管理和控制

import java.sql.*; //数据库语句和操作

import java.lang.System;//这个不知道

/*下面的这6个没有用,纯属写出来吓人*/

import javax.swing.tree.*;

import javax.swing.event.*;

import java.util.*;

import javax.swing.border.*;

import javax.swing.table.*;

import java.lang.String.*;

class Mywindow extends JFrame implements ActionListener//这个类implements actionlistener,意思就是它自己就可以执行actionListener的任务

{

JTextField txf=new JTextField(20); //建一个文字编辑框,长度20(只可以输入一行文字)

JTextArea jt=new JTextArea(10,30);//建一个文字编辑区域,长10宽30(可以回车然后输入多行文字)

JButton btn1=new JButton("查询");//建一个 查询 按钮

Mywindow()//构造函数,每个类必有的,可以为空

{

JFrame frm=new JFrame("Search");//建一个窗口(让其他的东西有地方可放,和容器一样。是3个最高级别的容器之一,其他两个是applet和window)

frm.setBounds(400,300,450,350);//设置大小和位置,前两个是坐标,后两个是大小

Container con=getContentPane();//建一个awt容器对象,用来添加其他元素,最好用这个添加元素。像:frm.add(all); 可以写成 con.add(all);

JPanel pnl4=new JPanel();//建一个面板用来添加其他元素(第二级别容器,最后需要被添加在frame上)

pnl4.setBorder(BorderFactory.createTitledBorder("Search"));//设置边框样式

pnl4.add(txf);//把文字编辑框添加到面板上

pnl4.add(btn1);//把按钮添加到面板上

btn1.addActionListener(this);//添加事件行为监听器(this),this意思是当前对象,呼应 implements ActionListener

JPanel pnl5=new JPanel();//同上

pnl5.setBorder(BorderFactory.createTitledBorder("Result"));//同上

jt.setWrapStyleWord(true);//这个忘了

jt.setLineWrap(true);//在区域规定的宽度下,如果文字的输入到一行最后则会自动令其一行继续,如果是(false),文字输入就会在这一行继续知道回车

pnl5.add(new JScrollPane(jt));//个这个面板添加右侧滚动条,当文字输入超过 长* 宽后 滚动条出现

JPanel all=new JPanel();//同上

all.setLayout(new BorderLayout());//设置布局,borderlayout()分东西南北(上下左右)中五个部分 无论窗口多大,中间占得面积最大

all.add(pnl4,BorderLayout.NORTH);//添加一个面板在上面

all.add(pnl5,BorderLayout.CENTER);//添加一个在中间

frm.add(all);//把最大的这个面板添加到窗口上 也可以用con.add(all);

frm.setVisible(true);//设置窗口显示属性 如果false就是不显示

frm.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});//加一个窗口监听 如果点小红叉关闭窗口则系统推出

}

public void actionPerformed(ActionEvent e)//作为ActionListener类的构造函数,如果你的class implements ActionListenser, 那就必须得有这个,也可以单独写一个class,不过有点麻烦

{

if(e.getSource()==btn1)//当按钮被点击的时候

{

String str="";//建一个字符串

String tmp=this.txf.getText();//同上,这个字符串的值是当前对象(窗口)中,文本框输入的值

for(int k=0;ktmp.length();k++)//建一个 永久循环

str+=tmp.charAt(k)+"%";//把 % 插入每一个字符后面, 作用后面说

String sql=null;//同上

Statement stmt=null;//定义一个stmt,用来建数据库连接的

sql="select * from chinese where charsound like'"+str+"'";//创建一个sql数据库语句,但它本身还是一个字符串

System.out.println(sql);//系统显示创建的语句,通常找错时候用的

try{//try 和 catch 的作用一句两句说不清楚 不知道你就自己查查

Class.forName("com.mysql.jdbc.Driver");//或者:Class.forName("org.gjt.mm.mysql.Driver");关联mysql数据库驱动

Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/japan?user=rootpassword=sa");//建立连接,数据库名japan(为什么不是chinese?)用户名root密码sa

stmt=conn.createStatement();//建立statement对象,用来发送sql语句到数据库

ResultSet rs=stmt.executeQuery(sql);//运行语句并建立一个查询结果的集合

System.out.println("\n------------------------search :"+str+"-------------------------------");//同上

jt.setText("");//清空文本编辑区域

while(rs.next())//while循环,当还有结果的时候,把所有查询结果添加加到文本编辑区域中

{

jt.append(new String(rs.getString("charname").getBytes("iso-8859-1"),"gb2312")+"\t");

System.out.print(new String(rs.getString("charname").getBytes("iso-8859-1"),"gb2312")+"\t");

}

stmt.close();//关闭关连,很重要。

}

catch(Exception eq){System.out.println("error");}

//--------------------------------------------------------------end btn1-------

}

}

public static void main(String args[])

{

Mywindow win=new Mywindow();//建立一个 mywindow 对象

win.pack();//将所有元素整合

win.show();

}

}

简易电子词典 无需数据库 用map添加单词及含义的 java源代码

public class Test4 {

static MapString, String map = new TreeMapString, String();

static {

map.put("watermelon", "西瓜");

map.put("banana", "香蕉");

map.put("strawberry", "草莓");

map.put("apple", "苹果");

}

public static void main(String[] args) {

System.out.println("请输入单词");

Scanner sc = new Scanner(System.in);

while (sc.hasNext()) {

String str1 = sc.nextLine();

if(str1.equals("退出")){

return;

}

else if (map.containsKey(str1)) {

System.out.println(map.get(str1));

} else{

System.out.println("次单词为新词,添加意思");

Scanner sc1 = new Scanner(System.in);

String str2=sc1.nextLine();

map.put(str1, str2);

System.out.println("添加成功。");

}

}

}

}

java代码如何实现中英文转换

使用java提供的国际化功能就可以了。不过建议你使用框架技术中的国际化,框架技术中的国际化都做过封装,实现起来相对简单的多。只需要简单的配置就可以实现中英文或者其他语言的切换了!~

谁会用java做一个英汉字典啊?或者有源代码也可以,求大神帮忙

lz  你好

代码还是比较简单  就是需要一个做好的txt英汉词典文档

以下是一个简单的例子:

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.io.*;

import java.util.*;

public class EC_Dictionary extends JFrame{

private JTextField input;

private JButton search;

private JTextArea output;

public EC_Dictionary(){

super("英汉词典");

input = new JTextField(14);

search = new JButton("查询");

search.setFont(new Font("宋体", Font.PLAIN, 15));

search.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

searchWords();

}

});

output = new JTextArea(10,18);

output.setEditable(false);

output.setFont(new Font("宋体", Font.PLAIN, 18));

output.setForeground(Color.RED);

setLayout(new FlowLayout(FlowLayout.CENTER, 5, 20));

getContentPane().add(input);

getContentPane().add(search);

getContentPane().add(output);

setSize(300,320);

setResizable(false);

setLocationRelativeTo(null);

setDefaultCloseOperation(3);

setVisible(true);

}

//查询单词

public void searchWords(){

try {

BufferedReader br = new BufferedReader(new FileReader("dictionary.txt"));

String line, inputWord;

boolean isFound = false;

inputWord = input.getText();

if(inputWord.equals("")){

return;

}

while((line = br.readLine()) != null){

Scanner in = new Scanner(line);

if(in.next().equals(inputWord)){

int offset = inputWord.length();

output.setText("\n\n\n\n"+line.substring(offset));

isFound = true;

break;

}

}

if(!isFound){

output.setText("没找到相应项..");

}

}

catch (Exception ex) {

ex.printStackTrace();

}

}

public static void main (String[] args) {

new EC_Dictionary();

}

}

运行效果:

ps:

本程序要用附件中dictionary.txt文档  lz要把源程序和这个文档放在同目录下  才能正常运行

希望能帮助你哈

我们老师有期末课程设计,关于Java源程序代码的英汉字典的,跪求您的帮忙我只有这么多的财富值,我可充话

package zyhz;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.sql.*;

import java.io.*;

import com.sun.media.sound.*;

public class Dictionary {

public Dictionary() {

}

public static void main(String[] args)

{

dicFrame frame = new dicFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

class dicFrame extends JFrame

{

public dicFrame()

{

setTitle("Dictionary");

setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);

panel = new JPanel();

add(panel);

//菜单栏

JMenu fileMenu = new JMenu("文件");

JMenuItem ECItem = new JMenuItem("英汉字典");

ECItem.addActionListener(new

AbstractAction(){

public void actionPerformed(ActionEvent event)

{

text2.setText("请在上面输入英文!!!!");

}

});

JMenuItem CEItem = new JMenuItem("汉英字典");

CEItem.addActionListener(new

AbstractAction(){

public void actionPerformed(ActionEvent event)

{

text2.setText("请在上面输入中文!!!!");

}

});

//备份词库

JMenuItem BackupItem = new JMenuItem("备份词库");

BackupItem.addActionListener(new

AbstractAction(){

public void actionPerformed(ActionEvent event)

{

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (ClassNotFoundException ce) {

System.out.println(ce);

}

url = "jdbc:odbc:test";

String fileName;

chooser = new JFileChooser();

frame = new JFrame();

chooser.setDialogTitle("备份词库为");

try {

flag = chooser.showSaveDialog(frame);

} catch (HeadlessException h) {

System.out.println("Save File Dialog ERROR!");

}

if (flag == JFileChooser.APPROVE_OPTION) {

f = chooser.getSelectedFile();

fileName = chooser.getName(f);

fileName = chooser.getSelectedFile().getPath();

try {

File saveFile = new File(fileName);

FileWriter fw = new FileWriter(saveFile);

try

{

con = DriverManager.getConnection(url);

s = con.createStatement();

rs = s.executeQuery("SELECT * from 字典 ");

while (rs.next()) {

String a = rs.getString("单词");

String b = rs.getString("词语解释");

text2.append(a+b+"\n");

}

rs.close();

} catch (Exception ea) {

text2.setText("查询数据失败");

}

fw.write(text2.getText());

fw.close();

} catch (IOException ec) {

text2.setText(fileName);

}

text2.setText("词库备份成功!");

}

}

});

fileMenu.add(ECItem);

fileMenu.add(CEItem);

fileMenu.add(BackupItem);

fileMenu.addSeparator();

//退出监听器

fileMenu.add(new

AbstractAction("退出")

{

public void actionPerformed(ActionEvent event)

{

System.exit(0);

}

});

JMenu editMenu = new JMenu("编辑");

JMenuItem addItem = new JMenuItem("添加词汇");

addItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));

//菜单栏增加监听器

addItem.addActionListener(new

AbstractAction(){

public void actionPerformed(ActionEvent event)

{

if(text1.getText().equals(""))

text2.setText("没有输入内容!请重新增加!!!");

else

{

String s=text1.getText().trim();

String s1=text2.getText().trim();

text2.setText(new add().jadd(text1.getText().trim(),text2.getText().trim()));

text2.append(s+"\n"+s1+";"+" "+"//"+"该内容已增加!!!");

}

}

});

//菜单栏修改监听器

JMenuItem updateItem = new JMenuItem("修改词汇");

updateItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,InputEvent.CTRL_MASK));

updateItem.addActionListener(new

AbstractAction(){

public void actionPerformed(ActionEvent event)

{

if(text1.getText().equals(""))

text2.setText("没输入所要修改的内容!请重新输入!!!!");

else{

String s=text1.getText().trim();

text2.setText(new update().jupdate(text1.getText().trim(),

text2.getText().trim()));

text2.append(s+" "+"//"+"该内容已修改!!!");

}

}

});

//菜单栏删除监听器

JMenuItem deleteItem = new JMenuItem("删除词汇");

deleteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK));

deleteItem.addActionListener(new

AbstractAction(){

public void actionPerformed(ActionEvent event)

{

if(text1.getText().equals(""))

text2.setText("没输入所要删除的内容!请重新输入!!!!");

else{

String s=text1.getText().trim();

text2.setText(new delete().jdelete(text1.getText().trim()));

text2.append(s+" "+"//"+"该内容已删除!!!");

}

}

});

editMenu.add(addItem);

editMenu.add(updateItem);

editMenu.add(deleteItem);

JMenu helpMenu = new JMenu("Help");

helpMenu.setMnemonic('H');

aboutItem = new JMenuItem("About");

aboutItem.setMnemonic('A');

aboutItem.addActionListener(new

AbstractAction(){

public void actionPerformed(ActionEvent event)

{

new guanyu();

}

});

helpMenu.add(aboutItem);

menubar = new JMenuBar();

setJMenuBar(menubar);

menubar.add(fileMenu);

menubar.add(editMenu);

menubar.add(helpMenu);

//工具栏

label = new JLabel("请输入:");

text1= new JTextField("");

selectButton = new JButton("查询");

//工具栏查询监听器

selectButton.addActionListener(new

AbstractAction(){

public void actionPerformed(ActionEvent event)

{

if(text1.getText().equals(""))

text2.setText("没有输入查询内容!请重新输入!!!");

else text2.setText(new select().jselect(text1.getText().trim()));

}

});

//工具栏增加监听器

addButton = new JButton("增加");

addButton.addActionListener(new

AbstractAction(){

public void actionPerformed(ActionEvent event)

{

if(text1.getText().equals(""))

text2.setText("没有输入内容!请重新增加!!!");

else

{

String s=text1.getText().trim();

String s1=text2.getText().trim();

text2.setText(new add().jadd(text1.getText().trim(),text2.getText().trim()));

text2.append(s+"\n"+s1+";"+" "+"//"+"该内容已增加!!!");

}

}

});

//工具栏修改监听器

updateButton = new JButton("修改");

updateButton.addActionListener(new

AbstractAction(){

public void actionPerformed(ActionEvent event)

{

if(text1.getText().equals(""))

text2.setText("没输入所要修改的内容!请重新输入!!!!");

else{

String s=text1.getText().trim();

text2.setText(new update().jupdate(text1.getText().trim(),

text2.getText().trim()));

text2.append(s+" "+"//"+"该内容已修改!!!");

}

}

});

//工具栏删除监听器

deleteButton = new JButton("删除");

deleteButton.addActionListener(new

AbstractAction(){

public void actionPerformed(ActionEvent event)

{

if(text1.getText().equals(""))

text2.setText("没输入所要删除的内容!请重新输入!!!!");

else{

String s=text1.getText().trim();

text2.setText(new delete().jdelete(text1.getText().trim()));

text2.append(s+" "+"//"+"该内容已删除!!!");

}

}

});

soundButton = new JButton("读音");

soundButton.addActionListener(new

AbstractAction(){

public void actionPerformed(ActionEvent event)

{

JavaSoundAudioClip player;

try{

strsound = text2.getText().trim();

FileInputStream ff = new FileInputStream("sound//"+strsound+".wav");

player = new JavaSoundAudioClip(ff);

player.play();

}

catch(Exception e) {

System.out.println("error");

e.printStackTrace();

}

}

});

//万年历

dateButton = new JButton("万年历");

dateButton.addActionListener(new

AbstractAction() {

public void actionPerformed(ActionEvent event)

{

new calendar();

}

});

toolbar = new JToolBar();

add(toolbar,BorderLayout.NORTH);

toolbar.add(label);

toolbar.add(text1);

toolbar.add(selectButton);

toolbar.add(addButton);

toolbar.add(updateButton);

toolbar.add(deleteButton);

toolbar.add(soundButton);

toolbar.add(dateButton);

text2 = new JTextArea(8,40);

text2.setLineWrap(true);

JScrollPane scroll = new JScrollPane(text2);

add(scroll,BorderLayout.CENTER);

//快捷菜单 剪切,复制,粘贴

JPopupMenu popup= new JPopupMenu();

text2.setComponentPopupMenu(popup);

popup.add(new

AbstractAction("剪切",new ImageIcon("cut.gif"))

{

public void actionPerformed(ActionEvent event)

{

text2.cut();

}

});

popup.add(new

AbstractAction("复制",new ImageIcon("copy.gif"))

{

public void actionPerformed(ActionEvent event)

{

text2.copy();

}

});

popup.add(new

AbstractAction("粘贴",new ImageIcon("paste.gif"))

{

public void actionPerformed(ActionEvent event)

{

text2.paste();

}

});

text1.setComponentPopupMenu(popup);

}

public static final int DEFAULT_WIDTH = 400;

public static final int DEFAULT_HEIGHT = 250;

private JPanel panel;

private JMenuBar menubar;

private JToolBar toolbar;

private JLabel label;

private JTextField text1;

private JTextArea text2;

private JButton selectButton;

private JButton addButton;

private JButton updateButton;

private JButton deleteButton;

private JButton soundButton;

private JButton dateButton;

private JMenuItem aboutItem;

private Connection con;

private String url;

private Statement s;

private ResultSet rs;

public BufferedWriter Buffer;

private JFileChooser chooser;

private File f;

private JFrame frame;

private int flag;

private boolean b1;

private String strsound;

}

//查询类

class select {

private Connection con;

private String url;

private PreparedStatement pstmt;

private String sql,zz;

private ResultSet rs;

select() {

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (ClassNotFoundException ce) {

System.out.println(ce);

}

url = "jdbc:odbc:test";

}

public String jselect(String str) {

try {

con = DriverManager.getConnection(url);

sql = "select 词语解释 from 字典 where 单词=?";

pstmt= con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

pstmt.setString(1,str);

rs = pstmt.executeQuery();

while (rs.next()) {

System.out.print(rs.getString(1));

zz=rs.getString(1);

System.out.println(" ");

}

rs.close();

pstmt.close();

con.close();

} catch (SQLException ce) {

System.out.println(ce);}

return zz;

}

}

//增加类

class add {

private Connection con;

private String url;

private PreparedStatement pstmt;

private String sql,sql1,zz;

private ResultSet rs;

add() {

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (ClassNotFoundException ce) {

System.out.println(ce);

}

url = "jdbc:odbc:test";

}

public String jadd(String str1,String str2) {

try {

con = DriverManager.getConnection(url);

sql = "insert into 字典 values(?,?)";

pstmt= con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

pstmt.setString(1,str1);

pstmt.setString(2,str2);

pstmt.executeUpdate();

Statement s = con.createStatement();

sql1 = "SELECT * FROM 字典 ";

rs = s.executeQuery(sql1);

while (rs.next()) {

System.out.print(rs.getString(1));

System.out.print(rs.getString(2));

System.out.println(" ");

}

rs.close();

pstmt.close();

con.close();

} catch (SQLException ce) {

System.out.println(ce);}

return zz;

}

}

//修改类

class update {

private Connection con;

private String url;

private PreparedStatement pstmt;

private String sql,sql1,zz;

private ResultSet rs;

update() {

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (ClassNotFoundException ce) {

System.out.println(ce);

}

url = "jdbc:odbc:test";

}

public String jupdate(String str1,String str2) {

try {

con = DriverManager.getConnection(url);

sql = "UPDATE 字典 SET 词语解释=? WHERE 单词=?";

pstmt= con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

pstmt.setString(1,str1);

pstmt.setString(2,str2);

pstmt.executeUpdate();

Statement s = con.createStatement();

sql1 = "SELECT * FROM 字典 ";

rs = s.executeQuery(sql1);

while (rs.next()) {

System.out.print(rs.getString(1));

System.out.print(rs.getString(2));

System.out.println(" ");

}

rs.close();

pstmt.close();

con.close();

} catch (SQLException ce) {

System.out.println(ce);}

return zz;

}

}

//删除类

class delete {

private Connection con;

private String url;

private PreparedStatement pstmt;

private String sql,sql1,zz;

private ResultSet rs;

delete() {

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (ClassNotFoundException ce) {

System.out.println(ce);

}

url = "jdbc:odbc:test";

}

public String jdelete(String str1) {

try {

con = DriverManager.getConnection(url);

sql = "delete 字典 WHERE 单词=?";

pstmt= con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

pstmt.setString(1,str1);

pstmt.executeUpdate();

Statement s = con.createStatement();

sql1 = "SELECT * FROM 字典 ";

rs = s.executeQuery(sql1);

while (rs.next()) {

System.out.print(rs.getString(1));

System.out.print(rs.getString(2));

System.out.println(" ");

}

rs.close();

pstmt.close();

con.close();

} catch (SQLException ce) {

System.out.println(ce);}

return zz;

}

}

还有个数据库,如果你能留个邮箱,我把整份程序传给你~希望能帮到你

数据结构课设:关于用java制作一个英汉词典

如果只是课设的话,你直接26个字母是26张表,

把用户传来的单词取首字母,判断属于 哪张表

然后select * from tableA where value =‘用户输入的单词’

把返回值处理显示呗~~~~~。

这么做,因为表比较少,会导致表中的单词量比较大,select速度也是问题。

但是因为是课设,就不考虑那么多了

不然再根据第二个字母,把每张表分成两张也可以 。

不涉及数据库的话,就是把对应的txt里的信息用IO流读出来,存在map中,key是单词,value是词条信息,直接用map.get(“用户单词”);


文章名称:中英字典源代码java,字典的代码
本文来源:http://cdkjz.cn/article/hdpgdd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220