?? 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 + -