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;
}
}