特别感谢海词http://dict.cn/
/*
*实现的功能:
*1.保存历史记录,并且可以回到任意历史记录
*2.有快捷键返回上次历史记录和下次历史记录
*3.能够导出个人单词簿
*4.对于给出的任意一句话,给相应的单词标注音标
*/
Dict.java文件
import java.net.*;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.browser.*;
import org.eclipse.jface.dialogs.*;
import org.eclipse.swt.dnd.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
public class Dict {
private List history;
private Text searchWord;
private String wordInHistory;
public Dict(Display display,Shell shell) {
//final Clipboard clipboard = new Clipboard(display);
final Browser browser = new Browser(shell, SWT.NONE);
browser.setBounds(116, 66, 366, 255);
final ToolBar toolBar = new ToolBar(shell, SWT.WRAP);
toolBar.setBounds(20, 19, 48, 22);
ToolItem backward = new ToolItem(toolBar, SWT.PUSH);
backward.setImage(new Image(display, "image/backward.gif"));
backward.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
browser.back();
}
});
ToolItem forward = new ToolItem(toolBar, SWT.PUSH);
forward.setImage(new Image(display, "image/forward.gif"));
forward.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
browser.forward();
}
});
searchWord = new Text(shell, SWT.BORDER);
searchWord.setBounds(83, 19, 199, 22);
final Button search = new Button(shell, SWT.NONE);
final Image searchImage = new Image(display, "image/filter.gif");
search.setText("search");
search.setBounds(296, 19, 87, 22);
search.setImage(searchImage);
search.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String word = searchWord.getText();
String[] wordList = word.split(" ", -2);
int length = wordList.length;
if (length == 1) {
String url = "http://dict.cn/mini.php?q=";
if (word == null || word.equals("")) {
MessageDialog.openInformation(new Shell(),"显示提示", "失败信息!"
+ '\n' + '\n' + "注意:待查单词不能为空!!!...");
} else {
url = url + word;
browser.setUrl(url);
history.add(word);
}
} else {
try {
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
XmlReader reader = new XmlReader();
for (int j = 0; j < length; j++) {
sp.parse(new InputSource("http://dict.cn/ws.php?q="
+ wordList[j]), reader);
}
} catch (Exception a) {
}
}
}
});
final Label label = new Label(shell, SWT.NONE);
label.setText(" History");
label.setBounds(20, 56, 76, 22);
history = new List(shell, SWT.BORDER);
history.setBounds(20, 84, 76, 237);
history.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
String word = history.getSelection()[0];
String url = "http://dict.cn/mini.php?q=";
url = url + word;
browser.setUrl(url);
}
});
final Button wordList = new Button(shell, SWT.NONE);
wordList.setText("导出个人单词本");
wordList.setBounds(395, 19, 87, 22);
wordList.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Txt txt = new Txt();
int count = history.getItemCount();
for(int i = 0;i < count;i ++){
wordInHistory = history.getItem(i);
txt.aLine(wordInHistory);
}
txt.finish();
}
});
}
}
Txt.java
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
public class Txt {
public File result = new File("wordList.txt");
public FileWriter writer;
public PrintWriter pw;
public Txt() {
try {
writer = new FileWriter("wordList.txt");
pw = new PrintWriter(writer);
} catch (IOException iox) {
System.err.println(iox);
}
}
public void aLine(String in) { // 写入一行
pw.println(in);
}
public void finish() { // 关闭输入流,将文字从缓存写入文件
try {
pw.flush();
writer.close();
} catch (IOException iox) {
System.err.println(iox);
}
}
}
XmlReader.java文件
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import java.io.UnsupportedEncodingException;
public class XmlReader extends DefaultHandler {
java.util.Stack tags = new java.util.Stack();
public String currentName;
public StringBuffer currentValue = new StringBuffer();
public XmlReader() {
super();
}
public void startElement(String uri, String localName, String qName,
Attributes attributes)
throws SAXException {
currentName = qName;
}
public void characters(char ch[], int start, int length)
throws SAXException {
XmlReader xmlreader = new XmlReader();
if(currentName=="pron"){
String pron = new String(ch, start, length);
System.out.println(pron);
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
}
}
test.java文件
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class test {
/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell(display);
shell.setSize(508, 377);
shell.setText("Dict --by stonestrange");
Dict dict = new Dict(display,shell);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
2008-05-03
订阅:
博文评论 (Atom)
没有评论:
发表评论