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张图片

程序界面截图如下:

Java课程设计 --- 词典笔记,对自己的单词本进行增删改查_第2张图片

Java课程设计 --- 词典笔记,对自己的单词本进行增删改查_第3张图片

Java课程设计 --- 词典笔记,对自己的单词本进行增删改查_第4张图片

主页界面:

Java课程设计 --- 词典笔记,对自己的单词本进行增删改查_第5张图片

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

Java课程设计 --- 词典笔记,对自己的单词本进行增删改查_第6张图片

Java课程设计 --- 词典笔记,对自己的单词本进行增删改查_第7张图片

Java课程设计 --- 词典笔记,对自己的单词本进行增删改查_第8张图片

你可能感兴趣的:(Java课程设计)