Java课程设计 --- 词典笔记,对自己的单词本进行增删改查

登陆界面是Login.java文件,登陆账号为001,密码为123。

Login.java

import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.*;
import java.awt.event.*;

public class Login extends JFrame{

	JPanel jp0,jp1,jp2,jp3;
	JLabel jlb1,jlb2;
	JButton jb1,jb2;
	JTextField jtf1,jtf2;
	JPasswordField jpf1;
	public void login() {
		jp0=new JPanel();
		jp1=new JPanel();
		jp2=new JPanel();
		jp3=new JPanel();
		
		jlb1=new JLabel("用户名");
		jlb2=new JLabel("密  码");
		jb1=new JButton("登录");
		jb2=new JButton("取消");
		
		jtf1=new JTextField(20);
		jpf1=new JPasswordField(20);
		//设置布局管理
		this.setLayout(new GridLayout(4,1));
		
		jp1.add(jlb1);
		jp1.add(jtf1);
		
		jp2.add(jlb2);
		jp2.add(jpf1);
		
		jp3.add(jb1);
		jp3.add(jb2);
		jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (jtf1.getText().equals("001")) {
                	if (String.valueOf(jpf1.getPassword()).equals("123")) {
                		JOptionPane.showMessageDialog(null, "登陆成功", "恭喜", JOptionPane.INFORMATION_MESSAGE);
                		setVisible(false);
                		Menu menu = new Menu();
                		menu.menu();
                		
                	} else {
                		JOptionPane.showMessageDialog(null, "用户名密码错误!", "提示", JOptionPane.ERROR_MESSAGE);
                	}
				}
            }
        });
		jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jtf1.setText(null);
                jpf1.setText(null);
            }
        });
		
		//加入到JFrame
		this.add(jp0);
		this.add(jp1);
		this.add(jp2);
		this.add(jp3);
		
		this.setTitle("登录界面");
		this.setSize(500,300);
		this.setLocationRelativeTo(null); 
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
		
		this.setVisible(true);
		this.setResizable(false);
		
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Font font = new Font("宋体",Font.PLAIN,20);
		UIManager.put("Button.font", font);
		UIManager.put("TextField.font", font);
		UIManager.put("PasswordField.font", font);
		UIManager.put("TextArea.font", font);
        UIManager.put("Menu.font", font);
        UIManager.put("MenuItem.font", font);
        Login log=new Login();
        log.login();
	}
}

登陆成功与失败会弹框提示。

登陆成功后创建主菜单对象。

Menu.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;

class Menu extends JFrame{
	//菜单
	JMenuBar menuBar=new JMenuBar();
	JMenu fileMenu,editMenu,helpMenu;
	JMenuItem fileEnglish,fileChinese,exit,editShow,editAdd,editMod,editDel,helpItem;
	JTextField inputText;
	JTextArea textArea;
	JLabel label1,label2;
	JButton btn1;
	JPanel p1,p2;
	
	Display display;
	Add add;
	Modify modify;
	Delete delete;
	
	Dictionary dic = new Dictionary();
	
	public void menu(){
		this.setJMenuBar(menuBar);
		fileMenu=new JMenu("文件(F)");
		editMenu=new JMenu("编辑(E)");
		helpMenu=new JMenu("帮助(H)");
		
		
		fileEnglish=new JMenuItem("英汉词典");
		fileChinese=new JMenuItem("汉英词典");
		exit=new JMenuItem("退出");
		
		editShow=new JMenuItem("显示词汇");
		editAdd=new JMenuItem("添加词汇");
		editMod=new JMenuItem("修改词汇");
		editDel=new JMenuItem("删除词汇");
		
		helpItem=new JMenuItem("使用帮助");
		
		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		menuBar.add(helpMenu);
		
		fileMenu.add(fileEnglish);
		fileMenu.add(fileChinese);
		fileMenu.addSeparator();
		fileMenu.add(exit);
		
		editMenu.add(editShow);
		editMenu.add(editAdd);
		editMenu.add(editMod);
		editMenu.add(editDel);
		
		helpMenu.add(helpItem);
		
		dic.load();
		
		
		fileEnglish.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	label1.setText("请输入要查询的单词:");
            }
        });
		
		fileChinese.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	label1.setText("请输入要查询的汉译:");
            }
        });
		
		editShow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                display = new Display();
                display.display();
                Word head = dic.show();
                Word p = head;
                int n = 0;
                while(head.next != null){
                	head = head.next;
            		n++;
            	}
                for(int i = 0; p.next != null && i < n; i++){
                	if(p.next.name.length()<6){
                		display.jta.append("\b"+p.next.name+'\t');
                	}
                	else {
                        display.jta.append("\b"+p.next.name);
                	}
                    display.jta.append("\t");
                    display.jta.append("\b"+p.next.explain);
                    display.jta.append("\n");
                    p = p.next;
                }
            }
        });
		
		editAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                add = new Add();
                add.add();
                add.btn.addActionListener(new ActionListener() {
                	public void actionPerformed(ActionEvent e) {
                		dic.add(add.jtf.getText(),add.jta.getText());
                		add.jtf.setText("");
                		add.jta.setText("");
                		JOptionPane.showMessageDialog(null, "添加成功", "恭喜", JOptionPane.INFORMATION_MESSAGE);
                	}
                });
            }
        });
		
        
		
		editMod.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                modify = new Modify();
                modify.modify();
                modify.btn.addActionListener(new ActionListener() {
                	public void actionPerformed(ActionEvent e) {
                		int i = dic.change(modify.jtf.getText(),modify.jta.getText());
                		if(i == 1){
                			modify.jtf.setText("");
                			modify.jta.setText("");
                    		JOptionPane.showMessageDialog(null, "修改成功", "恭喜", JOptionPane.INFORMATION_MESSAGE);
                		}else{
                			modify.jtf.setText("");
                			modify.jta.setText("");
                			JOptionPane.showMessageDialog(null, "没找到该单词,修改失败!", "提示", JOptionPane.ERROR_MESSAGE);
                		}
                	}
                });
            }
        });
		
		editDel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                delete = new Delete();
                delete.delete();
                delete.btn.addActionListener(new ActionListener() {
                	public void actionPerformed(ActionEvent e) {
                		if(dic.delete(delete.jtf.getText())){
                			JOptionPane.showMessageDialog(null, "删除成功", "恭喜", JOptionPane.INFORMATION_MESSAGE);
                		}else{
                			JOptionPane.showMessageDialog(null, "没找到该单词,删除失败!", "提示", JOptionPane.ERROR_MESSAGE);
                		}
                	}
                });
            }
        });
		
		exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	dic.save();
                System.exit(0);
            }
        });
		
		helpItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
	            JOptionPane.showMessageDialog(null, "查询:输入单词,点击查询,在文本框显示结果;\n" +
	                    "添加词汇:输入单词,在文本框输入解释,点击增添即可;\n" +
	                    "删除词汇:输入单词,点击删除即可将此单词及其注释删除;\n" +
	                    "修改词汇:输入单词,在文本框输入修改后的注释,点击修改;\n" +
	                    "显示词汇:会显示所有的排好序的词汇\n","帮助",JOptionPane.WARNING_MESSAGE);
	        }
		});

		inputText=new JTextField(20);
		textArea=new JTextArea(10,10);
		label1=new JLabel("请输入要查询的单词:");
		label2=new JLabel("   查询结果:");
		btn1=new JButton("查询");
//		btn2=new JButton("发音");
		
		btn1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				if(label1.getText().equals("请输入要查询的单词:")){
					if(dic.search1(inputText.getText()).equals("查找失败!")){
		                JOptionPane.showMessageDialog(null, "查找失败!","消息对话框",JOptionPane.WARNING_MESSAGE);
		            } else {
		            	textArea.setText(dic.search1(inputText.getText()));
		            }
				} else if(label1.getText().equals("请输入要查询的汉译:")) {
					if(dic.search2(inputText.getText()).equals("查找失败!")){
		                JOptionPane.showMessageDialog(null, "查找失败!","消息对话框",JOptionPane.WARNING_MESSAGE);
		            } else {
		            	textArea.setText(dic.search2(inputText.getText()));
		            }
				}
			}
		});
		
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
	            dic.save();
	            System.exit(0);
	        }
		});
		
		p1=new JPanel(new BorderLayout());
		p2=new JPanel(new FlowLayout(FlowLayout.LEFT,10,10));
		p2.add(label1);
		p2.add(inputText);
		p2.add(btn1);
//		p2.add(btn2);
		p1.add(label2,"North");
		p1.add(textArea,"Center");
		this.add(p2,"North");
		this.add(p1,"Center");
		
		this.setTitle("词典笔记");
		this.setSize(800,600);
		this.setLocation(200,200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
		
		this.setVisible(true); 
	}
}

class Display extends JFrame{

	JLabel label;
	JTextArea jta;
	JScrollPane scroll;
	JPanel p;
	
	public void display(){
		label = new JLabel("词汇展示如下:");
		jta = new JTextArea();
		scroll = new JScrollPane(jta);
		scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		p=new JPanel(new BorderLayout());
		p.add(label,"North");
		p.add(scroll,"Center");
		this.add(p);
		
		this.setTitle("显示词汇");
		this.setSize(600,400);
		this.setLocation(300,300);
		this.setVisible(true); 
	}
}

class Add extends JFrame{

	JLabel label1,label2;
	JTextField jtf;
	JTextArea jta;
	JButton btn;
	JPanel p1,p2;
	
	public void add(){
		label1 = new JLabel("请输入要添加的单词:");
		label2 = new JLabel("添加的汉译为:");
		jtf = new JTextField(20);
		jta = new JTextArea();
		btn = new JButton("确定");
		p1 = new JPanel(new BorderLayout());
		p1.add(label2,"North");
		p1.add(jta,"Center");
		p2 = new JPanel(new FlowLayout(FlowLayout.LEFT,10,10));
		p2.add(label1);
		p2.add(jtf);
		p2.add(btn);
		this.add(p2,"North");
		this.add(p1,"Center");
		
		this.setTitle("添加词汇");
		this.setSize(600,400);
		this.setLocation(300,300);
		this.setVisible(true);
	}
}

class Modify extends JFrame{

	JLabel label1,label2;
	JTextField jtf;
	JTextArea jta;
	JButton btn;
	JPanel p1,p2;
	
	public void modify(){
		label1 = new JLabel("请输入要修改的单词:");
		label2 = new JLabel("修改的汉译为:");
		jtf = new JTextField(20);
		jta = new JTextArea();
		btn = new JButton("确定");
		p1 =new JPanel(new BorderLayout());
		p1.add(label2,"North");
		p1.add(jta,"Center");
		p2=new JPanel(new FlowLayout(FlowLayout.LEFT,10,10));
		p2.add(label1);
		p2.add(jtf);
		p2.add(btn);
		this.add(p2,"North");
		this.add(p1,"Center");
		
		this.setTitle("修改词汇");
		this.setSize(600,400);
		this.setLocation(300,300);
		this.setVisible(true); 
	}
}

class Delete extends JFrame{

	JLabel label;
	JTextField jtf;
	JButton btn;
	JPanel p,p1,p2;
	
	public void delete(){
		label = new JLabel("请输入要删除的单词:");
		jtf = new JTextField(20);
		btn = new JButton("确定");
		p=new JPanel(new FlowLayout(FlowLayout.LEFT,10,10));
		p1=new JPanel(new FlowLayout());
		p2=new JPanel(new FlowLayout());
		p.add(label);
		p.add(jtf);
		p.add(btn);
		this.add(p1,"North");
		this.add(p,"Center");
		this.add(p2,"South");
		
		this.setTitle("删除词汇");
		this.setSize(600,400);
		this.setLocation(300,300);
		this.setVisible(true); 
	}
}

Dictionary.java中包含对单词进行增删改查的方法。

Dictionary.java

import java.io.*;

public class Dictionary {
	
    Word head;
    
    public Dictionary(){
        head = new Word(null);
    }
    
    public Word show(){
    	
    	Word p;
    	Word q;
    	Word temp = new Word(null);
    	
	    for(p=head.next; p!=null; p=p.next){
			for(q=p.next; q!=null; q=q.next){
				if(p.name.compareTo(q.name)>0){
					temp.name = p.name;
					temp.explain = p.explain;
					p.name = q.name;
					p.explain = q.explain;
					q.name = temp.name;
					q.explain = temp.explain;
				}
			}
		}
    	return head;
    }
    
    public void add(String n,String e){
    	
    	Word word = new Word(null);
    	word.name = n;
    	word.explain = e;
    	Word p = head;
    	
    	while(p.next != null){
    		p = p.next;
    	}
    	p.next = word;
    }
    
    public String search1(String n){
        Word p = head;
        int i = 0;
        while(p.next != null){
            p = p.next;
            if(p.name.equals(n)){
            	i = 1;
                return p.explain;
            }
        }
        if(i == 0){
            return "查找失败!";
        }
        return n;
    }
    
    public String search2(String n){
        Word p = head;
        int i = 0;
        while(p.next != null){
            p = p.next;
            if(p.explain.equals(n)){
            	i = 1;
                return p.name;
            }
        }
        if(i == 0){
            return "查找失败!";
        }
        return n;
    }
    
    public int change(String n,String e){
        Word p = head;
        Word q = p.next;
        int i = 0;
        while(p.next != null){
            if(q.name.equals(n)){
                q.explain = e;
                i = 1;
                break;
            }
            p = q;
            q = p.next;
        }
        return i;
    }
    
    public boolean delete(String n){
        Word p = head;
        Word q = p.next;
        while(p.next != null){
            if(q.name.equals(n)){
                p.next = q.next;
                return true;
            }
            p = q;
            q = p.next;
        }
        return false;
    }

    public void load(){
        File file = new File("dictionary.txt");
        Reader in;
        try {
        	if(!file.exists()){
        		file.createNewFile();
        	}
            in = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(in);
            String str = null;

            while((str = bufferedReader.readLine()) != null){
                Word p = new Word(null);
                p.name = str;
                if((str = bufferedReader.readLine()) != null){
                    p.explain = str;
                    p.next = head.next;
                    head.next = p;
                }
            }
//          if((str = bufferRead.readLine()) != null){
//          	p.redio = str;
//          	p.next = head.next;
//          	head.next = p;
//      	}
            bufferedReader.close();
            in.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void save(){
        File file = new File("dictionary.txt");
        try {
            Writer out = new FileWriter(file);
            BufferedWriter bufferedWriter = new BufferedWriter(out);
            Word p = head;
            Word q = p.next;
            while(p.next != null){
            	bufferedWriter.append(q.name);
            	bufferedWriter.newLine();
            	bufferedWriter.append(q.explain);
            	bufferedWriter.newLine();
                p = q;
                q = p.next;
            }
            out.flush();
        	bufferedWriter.flush();
            bufferedWriter.close();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}
}
Word.java包括对单词的set()和get()方法。
public class Word {
	
    String name;
    String explain;
//  String redio;
    Word next;
    
    public Word(Word next){
        this.next = next;
    }
    
    public Word(String name,String explain,Word next){
        this.name = name;
        this.explain = explain;
        this.next = next;
    }
    
    public Word getNext(){
        return this.next;
    }
    
    public void setNext(Word next){
        this.next = next;
    }
    
    public String getName(){
        return this.name;
    }
    
//  public String getRidio(){
//  return redio;
//}
    
    public void setName(String name){
        this.name = name;
    }
    
    public String getExplain(){
        return this.explain;
    }
    
    public void setExplain(String explain){
        this.explain = explain;
    }
}

文件数据是用dictionary.txt文本文件存储的。注意:应该一行英文对应一行汉语翻译!

文本截图如下:


程序界面截图如下:


主页界面:


三个导航对应相应功能,截图如下:




课 程 设 计 报 告 课程设计名称 Java语言程序设计 专 业 信息管理与信息系统 目录 一、设计分析………………………………………………1 二、数据流程图 ………………………………………… 1 三、操作方法及试验结果…………………………………2 1、菜单窗口 …………………………………………2 2、查询 ………………………………………………2 3、添加 ………………………………………………3 4、修改 ………………………………………………3 5、删除 ………………………………………………4 6、帮助 ………………………………………………4 设计体会……………………………………………5 一、设计分析 英汉电子词典作为一个常用的学习工具,系统应该能完成词典功能。该系统主要用于 实现英汉互译和编辑词典库中的信息的功能,系统拥有自己的数据库。 二、数据流程图 (用以各功能之间的关系) 三、操作方法及试验结果 此窗口为菜单窗口,用户可在此窗口内选择应用的内容。其中文件目录中包括:英汉词 典,汉英词典,备份文库。编辑目录中包括:添加,修改,删除。 用户可在窗口内输入要查询的单词或中文意思,输入完毕后点击查询将出现次单词的解 释或中文对应的单词。如输入的单词不存在词库中,则显示查无此单词。 (3)若用户需要在词库中添加新的单词,则选择单词的添加。用户可输入想要添加的单 词及其解释。单击确定后,次单词保存在词库中。 用户想要修改词库中已存在的单词,可选择单词的修改。用户可对该单词及其解释进行 修改。修改并保存后单击确定,系统将提示用户记录修改成功。 (5)想要删除词库中已存在的单词,可选择单词删除。输入想要删除单词,点击确 定后,此单词及其解释一并删除删除后,系统提示用户单词删除成功,若没有找到要 删除单词,系统提示用户不存在此单词。 (6)用户可点击帮助来更好的了解电子词典功能及其用法。 设计体会 经过这次不到两周的课程设计真的让我们感触颇多啊!首先,知识方面,经过大二上 学期一学期的学习,让我们对java的基础知识和操作环境和平台有了初步的了解和认识 ,但是这次设计中还是遇到了很多问题,尤其是数据库的创建和连接部分,由于以前接 触这部分知识比较少,这次用到时感觉很陌生,我们在图书馆查阅了很多资料,并上网 查询一些相关资料,功夫不负有心人,最后大家的努力终于看到了成果,这次成功不仅 让我们的课程设计得以成功,更重要的是掌握了更多以前不熟悉的知识,为以后的学习 和实践打下基础。 然后,操作过程,在这次课程设计的开始就有机会自己下载和安装jdk和eclipse进一 步熟悉java的操作平台,并且在项目的建设和代码的导入以及运行和改错的过程中进一 步了解和巩固了基础的知识,运行和改错对我们而言也是学习与复习的好机会,是它们 使我学会了如何自学、如何查阅资料等,让我学习了很多,也让我对知识点有了更深的 理解。 最后,通过这次课程设计让我深刻体会到合作的重要性,众人划桨开大船,大家分工合 作,查阅资料,讨论纠错,程序的编写运行才得以顺利实现,更重要的是大家在讨论和 沟通中学到了更多的知识,共同进步。同时,这个过程也让我们发现了自己相关知识的 不足,在以后的学习实践中,我们会通过各种方式提高自己相关方面的知识,为以后的 学习和实践打下基础。 ----------------------- java课程设计---英汉电子词典全文共7页,当前为第1页。 java课程设计---英汉电子词典全文共7页,当前为第2页。 开始 显示菜单 输入选项 显示无法找到 显示删除成功 显示无法找到 显示修改成功 输入单词 输入删除单词 输入修改的单词 显示查询选项 输入中文 输入显示信息 判断 循环体 查询 判断 循环体 查询 判断 判断 输入英文单词 输入中文意思 判断 判断 循环体 循环体 计算 计算 判断 判断 显示无法找到 显示无法找到 显示查找信息 显示查找信息 结束 java课程设计---英汉电子词典全文共7页,当前为第3页。 java课程设计---英汉电子词典全文共7页,当前为第4页。 java课程设计---英汉电子词典全文共7页,当前为第5页。 java课程设计---英汉电子词典全文共7页,当前为第6页。 java课程设计---英汉电子词典全文共7页,当前为第7页。
1. 输出50—100间的所有素数,其中判断一个数是否为素数用函数完成。 2. 设计一个学生成绩管理系统,能输入学生的学号、姓名和成绩等数据,能按成绩从高到低进行排序,并能将排序的结果输出。 提示: 设计一个学生类student,包含三个私有数据成员,即学号sno、姓名sname和成员score; 在学生类student中再设计三个方法 setdata、display和getscore,分别用于完成输入学生信息、输出学生信息、返回某个学生成绩的功能; 定义一个manage类,它有一个student类型的私有数据成员s[num],其中num表示学生人数; 在manage类中分别定义三个方法input、output、sort,分别用于完成输入、输出和排序功能。 3.一个电子产品商店里卖各种电子产品,以下是几种产品类信息。 产品Product 的属性信息包括: 产品号(number) 种类(category) 名称(name) 价格(price ) 计算机Computer除具有产品基本信息外,还可能具有下面的属性: 内存(memory) 处理器(processorName)   笔记本电脑Laptop 除具有产品基本信息外,还可能具有下面的属性: 厚度(thickness) 重量(weight) 根据以上信息,首先抽象出类Product,它派生出子类Computer,Computer又派生出它的子类Laptop,实现以上三个类的定义,定义相应的构造方法,声明一个测试类,生成类对象,并把打印结果显示在屏幕上。
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值