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

📄 dic.java

📁 A vocabulary dictionary
💻 JAVA
字号:
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.io.*;


public class Dic extends JFrame
{

    HashMap map=new HashMap();
    private String word,entry;
    private JButton add = new JButton("Add Word");
    private JButton sy = new JButton("Add Synonym");
    private JButton delete = new JButton("Delete");
    private JButton search = new JButton("Search");
    private JButton showAll = new JButton("Show All");
    private JButton exit = new JButton("Exit"); 
    JLabel w = new JLabel(" Word ");
    JLabel s = new JLabel(" Synonym");
    JLabel d = new JLabel(" display");
    JTextArea display = new JTextArea(10, 20);
    JTextField w1 = new JTextField(10);
    JTextField s1 = new JTextField(10);
    
////////////////////////////////////////////////////
    public Dic()
    {
     addWindowListener( new WindowEar() );
        setSize(350, 400);
        getContentPane().setLayout(new FlowLayout() );

                add(w);
                add(w1);
                add(s);
                add(s1);
                add(d);
               add(display);
                add(add);
                add(sy);
                add(delete);     
                add(search);
                add(showAll);
                add(exit);
                display.setLineWrap(true);

        MyActionListener actionEar = new MyActionListener();
        exit.addActionListener(actionEar);
        search.addActionListener(actionEar);
        sy.addActionListener(actionEar);
        delete.addActionListener(actionEar);
        add.addActionListener(actionEar);
        showAll.addActionListener(actionEar);
    }
//////////////////////////////////////////////////
 class WindowEar extends WindowAdapter {
       public void windowClosing(WindowEvent e) {//Invoked when a window is in the process of being closed
               System.exit(0);
           }
    }
//////////////////////////////////////////////////
 class MyActionListener implements ActionListener {
            //implement event-handling method
            public void actionPerformed(ActionEvent e) {//Invoked when an action occurs.
                String buttonLabel = ((JButton)e.getSource()).getText();
        
        if(buttonLabel.equals("Exit"))
            System.exit(0);
        
       //------------------------------------
        if(buttonLabel.equals("Delete") ) {
           if(map.isEmpty())
                display.append(" The dictionary is Empty..");
              else{  
               map.remove(word);
               display.append("Your word has been deleted") ;}
        }// if Delete
       //------------------------------------
        if(buttonLabel.equals("Search") ) {
            if(word.equals(""))
                  display.append("Re-enter a word...");
              else{
                 if(map.containsKey(word)){
                    String result=map.get(word).toString();
                     if(result==null)
                        display.append("Your word exists without associated meanings...");
                     else
                        display.append(word+" = "+result);}
                 else     
                     display.append("The word does not exist....");
        }// if Search
        //------------------------------------
        if(buttonLabel.equals("Add Synonym") ) {
            if(word.equals("") && entry.equals("")) 
                    display.append("Re-enter a word and synonum/s....");
                 else if(word.equals(""))
                    display.append("Re-enter a word...."); 
                 else{   
                     if(map.containsKey(word)){
                        ArrayList valueMeanings=(ArrayList) map.get(word);
                        Scanner sc=new Scanner(entry);
                          while(sc.hasNext())
                              valueMeanings.add(sc.next());
                         display.append("Your synonum has been added...");} 
                     else{
                        ArrayList meaning=(ArrayList) map.get(word);
                        Scanner sc=new Scanner(entry);
                          while(sc.hasNext())
                              meaning.add(sc.next());
                        map.put(word,meaning);
                  display.append("Your word & synonum have been added...." );}}
                 
        }// if Add Synonym
        //------------------------------------
        if(buttonLabel.equals("Add Word") ) {
            if(word.equals(""))
                  display.append("Re-enter a word....");
                
             else{ 
                  if(!map.containsKey(word)){
                     ArrayList meaningsList=new ArrayList();
                     map.put(word,meaningsList);
                     display.append("Your word has been added....");}  
                 
                  else
                     display.append("Your word already exists.....");}}
        }// if Add Word
         
         if(buttonLabel.equals("Show All") ) {
            if(map.isEmpty()){
                display.append(" The dictionary is Empty..");}
            else{
               display.append(word+" = "+map.get(word)+"\n");}
          }// if Show All
        }}
////////////////////////////////////////////////////
}//end of class..

⌨️ 快捷键说明

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