📄 dictionaryframe.java
字号:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/**
* Sample application using Frame.
*
* @author
* @version 1.00 07/04/19
*/
public class DictionaryFrame extends Frame implements MouseListener,KeyListener,TextListener{
////////////////////////////////////////////////
//定义区
//////////////////////////////////////////////////
final int WORDCOUNT = 57510;//总单词数
final String Info = "本程序是此次Java小作业之一 英汉字典\n操作方法:\n在上面的输入框中输入要查的单词,若查到,此处显示解释,且右边List控件指到该词.\n若没找到,此处显示可能的单词,右边指向和其匹配最靠近的单词,按Enter键获得最靠近的单词;\n在输入句子的情况下(存在两个以上空格),输完后,按下回车键,本程序将会一一分析其中的单词\nCoded by 张天明 041221120\n";
//Label l = new Label("输入:");
TextField InputBox = new TextField("");//输入框
TextArea OutputBox = new TextArea(Info);//输出框
JList lst;//List框
String LastWord = new String ("");//保存前一次输入的词;
int FirstPoint = 0;//以某一个字母开头的单词的起始索引
int RecentPoint = 0;//当前字母的位置
int NowPoint = -1;//保存前一次查出的字母的位置
int NextPoint = 0;//以某一个字母开头的单词的结束索引
//文件中指针
String []Word = new String[WORDCOUNT];
int []off = new int [WORDCOUNT];
int []len = new int [WORDCOUNT];//将索引中的单词及其偏移,长度保存在数组中
//boolean ChangedList = false;
boolean ChangedText = false;//防止List框和输入框相互影响
boolean Sentence = false;
///////////////////////////////////////////////
//定义区
/////////////////////////////////////////////////
public DictionaryFrame() throws Exception
{
setTitle("Dictionary");
setSize(new Dimension(600, 400));//初始化界面
//this.setLayout(BorderLayout);
Font f = new Font("Tahoma",0,13);
setFont(f);
InIdx();//读入索引
lst = new JList(Word);//构建List
this.add(InputBox,BorderLayout.NORTH);
this.add(OutputBox,BorderLayout.EAST);
this.add(new JScrollPane(lst),BorderLayout.CENTER);
//将几个控件排好
InputBox.addTextListener(this);//监听输入框
InputBox.addKeyListener(this);
lst.addMouseListener(this);//监听List框
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
DictionaryFrame.this.windowClosed();
}
}
);
}
/**
* Invoked when a key has been typed.
* See the class description for {@link KeyEvent} for a definition of
* a key typed event.
*/
public void keyTyped(KeyEvent e)
{
////////////////////////////////////////////////////////////////
////对回车键进行判断.
if(e.getSource()==InputBox&&e.getKeyChar() =='\n')
{
if(Sentence)//若是句子,分析;
{
try
{
int t1= RecentPoint;
int t2 = NowPoint;
int t3 = NextPoint;//存入现在指针位置;
int t4 = FirstPoint;
NowPoint = -1;
String out = new String ("该句子由以下单词组成:\n");
String s = InputBox.getText();
int lenth = s.length();
String w[] = new String [100];
int n = 0;
byte a[] = s.getBytes();
byte b[] = new byte [100] ;
int l = 0;
for(int i =0;i<lenth;i++)//将单词一一分开
{
if(a[i] == (byte)' ' )
{
if(l!=0)
{
w[n] = new String(b,0,l);
n++;
l =0;
}
}
else
{
b[l] = a[i];
l++;
}
}
if(l!=0)
w[n] = new String(b,0,l);
else n--;
for(int i=0;i<=n;i++)//显示单词
{
out+= new String("第 "+(i+1)+" 个单词: "+w[i]+"\n");
}
String divide = "\n\n-------------------------我是分割线-------------------------\n\n";
out+=divide;
//String Meaning[] = new String [n+1];
for(int i=0;i<=n;i++)
{
if(isExist(w[i]))
{
out+= new String(w[i]+" 解释如下:"+"\r\n");
out+= getMeaning(off[RecentPoint],len[RecentPoint]);
out+= divide;
}
else
{
out+= new String (w[i]+" 不存在!"+divide);
}
}
/*
for(int i=0;i<=n;i++)
{
OutputBox.append(Meaning[i]);
}
*/
OutputBox.setText(out);
RecentPoint =t1;
NowPoint =t2;
NextPoint = t3;//恢复指针
FirstPoint = t4;
}
catch(Exception ex)
{
System.err.println(ex);
}
}
else
{
//若不是句子,获得待选的词;
InputBox.setText(Word[lst.getSelectedIndex()]);
}
}
}
/**
* Invoked when a key has been pressed.
* See the class description for {@link KeyEvent} for a definition of
* a key pressed event.
*/
public void keyPressed(KeyEvent e)
{
}
/**
* Invoked when a key has been released.
* See the class description for {@link KeyEvent} for a definition of
* a key released event.
*/
public void keyReleased(KeyEvent e)
{
}
/**
* Invoked when the mouse button has been clicked (pressed
* and released) on a component.
*/
public void mouseClicked(MouseEvent e)
{
//valueChanged(e);
}
/**
* Invoked when a mouse button has been pressed on a component.
*/
public void mousePressed(MouseEvent e)
{
}
/**
* Invoked when a mouse button has been released on a component.
*/
public void mouseReleased(MouseEvent e)
{
if(e.getSource()==lst)//确定消息来源为List框,则执行ListChanged().
{
ListChanged();
}
}
/**
* Invoked when the mouse enters a component.
*/
public void mouseEntered(MouseEvent e)
{
}
/**
* Invoked when the mouse exits a component.
*/
public void mouseExited(MouseEvent e)
{
}
public void textValueChanged(TextEvent e)
{
if(e.getSource()==InputBox)
{
if(ChangedText)
{
ChangedText = false;
return;
}//防止List框和输入框相互影响
try
{
String W = InputBox.getText();//获得输入框结果
///////////////////////////////////////////
////以下这段用于判断输入的是否是句子.特点是有两个以上的空格
int blank = 0;
int length = W.length();
for(int i = 0;i<length;i++)
{
if(W.charAt(i) == ' ') blank ++;
}
if (blank >=2)
{
OutputBox.setText("输入的应该是句子,不再进行单词判断!");
Sentence = true;
return ;
}
///////////////////////////////////////////////
if(W.length() == 0)//若长度为0,重新初始化
{
RecentPoint = 0;
lst.setSelectedIndex(0);
lst.ensureIndexIsVisible(0);
OutputBox.setText(Info) ;
LastWord = W;
return ;
}
else
//本来我是保存RecentPoint以加速查找的,但是发现对速度影响不大,所以去掉了
//if(W.length() == 1||W.length() < LastWord.length()||W.charAt(0)!=LastWord.charAt(0))//长度为1或比前一次小,重新获得初始偏移
{
RecentPoint = getFirst(W);
}
if(getWord(W)) //在前一次查询的基础上查这次的词
OutputBox.setText(getMeaning(off[RecentPoint],len[RecentPoint]));//查到输出结果
else // System.out.print(RecentPoint);
OutputBox.setText(getAlike(W)) ;//未查到输出可能的错误单词
if(NowPoint != RecentPoint)//如果偏移位置未变,即LIst框指向位置不变
{
lst.setSelectedIndex(RecentPoint);
lst.ensureIndexIsVisible(WORDCOUNT-1);
lst.ensureIndexIsVisible(RecentPoint);//先将位置放在最后,在放在当前单词上,可以让其排在第一位
NowPoint = RecentPoint;
}
LastWord = W;//报存单词
return ;
}
catch(Exception ex)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -