背单词系统

import java.io.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class wordtest

{   

   public static void main(String[] s)

   {

       guiView c =new guiView();

       c.showWindow("我爱背单词");

   }

}

 

class guiView extends JFrame

{

   JMenuBar menubar;

   JMenu menu;

   JMenuItem itemExit;

 

   public JTextField input;

   public JList list;

   public JTextArea text;

 

   public String[] Dictionary;

 

   public void showWindow(String s)

   {

      setTitle(s);

      setSize(400,300);

      setLocation(120,120);

      setResizable(false);

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     

 

      getContentPane().setLayout(new FlowLayout());

 

      //以下代码设计菜单结构

      menubar=new JMenuBar();

      menu=new JMenu("系统");

      itemExit=new JMenuItem("退出");

      menu.add(itemExit);

      menubar.add(menu);

      setJMenuBar(menubar);

 

      //以下代码设计词典主界面

      JLabel lb = new JLabel("请输入选中单词的英文拼写:");

      getContentPane().add(lb);

 

      input=new JTextField(18);

      input.addActionListener(new handler(this));

      getContentPane().add(input);

 

      readDict dict = new readDict();

      list=new JList(dict.getDicts("dict.txt"));

      Dictionary = dict.getDictionary();

      getContentPane().add(new JScrollPane(list));

 

      list.addMouseListener(new handler(this));

      list.setSelectedIndex(0); 

 

      text=new JTextArea(9,20);

      text.setEditable(false);

      text.setLineWrap(true);

      text.setWrapStyleWord(true);

      text.setText("*欢迎来到我爱背单词*");

      getContentPane().add(new JScrollPane(text));

 

      setVisible(true);

 

    

  }

 

 

}

 

 

class handler extends MouseAdapter implements ActionListener

{

    guiView gui;

    static String s1;

    static String s2;

    public handler(guiView gui) {this.gui=gui;}

 

    public void mouseClicked(MouseEvent e)

    {

        JList list=(JList)e.getSource();

        int index = list.locationToIndex(e.getPoint());

        s1=gui.Dictionary[index];

       

    }

 

    public void actionPerformed(ActionEvent e)                 

    {

        s2=gui.input.getText(); gui.text.setText(s2+s1);

        if(s2.equals(s1))

        {

             gui.text.setText("恭喜,您的拼写输入完全正确!");

        }

        else

        {

             gui.text.setText("抱歉,输入错误!"+'/n'+"正确答案是:"+s1);

        }

       

    }

 

}

class readDict

{

  String[] Dictionary;

 

  public String[] getDictionary() {return Dictionary;}

 

  public String[] getDicts(String fname)

  {

     String[] lists=null;

     try{

 

       BufferedReader br = new BufferedReader(new FileReader(fname));

 

       String s=br.readLine();

       int count=Integer.parseInt(s);

       Dictionary = new String[count];

       lists=new String[count];

 

       for(int i=0;i<count;i++)

       {

          s=br.readLine();

          StringTokenizer fx = new StringTokenizer(s," ");

          if(fx.hasMoreTokens()) lists[i]=new String(fx.nextToken());

          if(fx.hasMoreTokens()) Dictionary[i]=new String(fx.nextToken());

       }

 

     }catch(IOException e)

      {

          System.out.println(e);

      }

 

      return lists;

  }

}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值