显示标签为“coding_java”的博文。显示所有博文
显示标签为“coding_java”的博文。显示所有博文

2008-05-03

迷你小词典升级版

特别感谢海词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-04-22

迷你小词典~

原创小词典~~~~~~~~~使用了海词的提供的API~~~~~~~~在此感谢啦~~~~~~~~
import org.eclipse.swt.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.browser.Browser;

public class Dict {

private Text express;

private Text searchWord;

public Dict() {

final Display display = Display.getDefault();

final Shell shell = new Shell(display);
shell.setSize(500, 380);
shell.setText("Dict --by stonestrange");

final Browser browser = new Browser(shell, SWT.NONE);
browser.setBounds(116, 66, 363, 282);

searchWord = new Text(shell, SWT.BORDER);
searchWord.setBounds(91, 19, 268, 22);

final Button search = new Button(shell, SWT.NONE);
search.setText("search");
search.setBounds(385, 19, 94, 22);
search.setImage(searchImage);
search.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String word = searchWord.getText();
String url = "http://dict.cn/mini.php?q=";
url = url + word;
browser.setUrl(url);
}
});


express = new Text(shell, SWT.BORDER);
express.setBounds(116, 66, 363, 282);

shell.open();
shell.layout();

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}

public static void main(String[] args) {
new Dict();
}
}

2008-04-07

java解析xml

在这段代码中~~~~我解析的是海词提供的xml~~~~~~~~~~~~~运行环境是Eclipse~~~~~~~~~~不过在编码部分还存在一定的问题~~~~~~~~~有待努力~~~~~~~继续提高~~~~~~~~~~嘿嘿~~~~~~~~~~~~~~~

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;

public class MyXMLReader extends DefaultHandler {

java.util.Stack tags = new java.util.Stack();

private String currentName;

private StringBuffer currentValue = new StringBuffer();

public MyXMLReader() {
super();
}

public void startElement(String uri, String localName, String qName,
Attributes attributes)

throws SAXException {

}

public void characters(char ch[], int start, int length)
throws SAXException {
System.out.println(new String(ch,start,length));
}

public void endElement(String uri, String localName, String qName) throws SAXException {

//props.put(qName.toLowerCase(), currentValue.toString().trim());

}



public static void main(String args[]) {
try {
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
MyXMLReader reader = new MyXMLReader();
sp.parse(new InputSource("http://dict.cn/ws.php?q=stone"), reader);
} catch (Exception e) {
//e.printStackTrace();
}
}
}

[转]在Java中调用C语言生成的DLL文件

一、JAVA中所需要做的工作

在JAVA程序中,首先需要在类中声明所调用的库名称,如下:

static {

System.loadLibrary(“goodluck”);

}

在这里,库的扩展名字可以不用写出来,究竟是DLL还是SO,由系统自己判断。

还需要对将要调用的方法做本地声明,关键字为native。并且只需要声明,而不需要具体实现。如下:

public native static void set(int i);

public native static int get();

然后编译该JAVA程序文件,生成CLASS,再用JAVAH命令,JNI就会生成C/C++的头文件。

例如程序testdll.java,内容为:

public class testdll

{

static

{

System.loadLibrary("goodluck");

}

public native static int get();

public native static void set(int i);

public static void main(String[] args)

{

testdll test = new testdll();

test.set(10);

System.out.println(test.get());

}

}

用javac testdll.java编译它,会生成testdll.class。

再用javah testdll,则会在当前目录下生成testdll.h文件,这个文件需要被C/C++程序调用来生成所需的库文件。

二、C/C++中所需要做的工作

对于已生成的.h头文件,C/C++所需要做的,就是把它的各个方法具体的实现。然后编译连接成库文件即可。再把库文件拷贝到JAVA程序的路径下面,就可以用JAVA调用C/C++所实现的功能了。

接上例子。我们先看一下testdll.h文件的内容:

/* DO NOT EDIT THIS FILE - it is machine generated */

#include

/* Header for class testdll */

#ifndef _Included_testdll

#define _Included_testdll

#ifdef __cplusplus

extern "C" {

#endif

/*

* Class: testdll

* Method: get

* Signature: ()I

*/

JNIEXPORT jint JNICALL Java_testdll_get

(JNIEnv *, jclass);

/*

* Class: testdll

* Method: set

* Signature: (I)V

*/

JNIEXPORT void JNICALL Java_testdll_set

(JNIEnv *, jclass, jint);

#ifdef __cplusplus

}

#endif

#endif

在具体实现的时候,我们只关心两个函数原型

JNIEXPORT jint JNICALL Java_testdll_get (JNIEnv *, jclass);

JNIEXPORT void JNICALL Java_testdll_set (JNIEnv *, jclass, jint);

这里JNIEXPORT和JNICALL都是JNI的关键字,表示此函数是要被JNI调用 的。而jint是以JNI为中介使JAVA的int类型与本地的int沟通的一种类型,我们可以视而不见,就当做int使用。函数的名称是JAVA_再加 上java程序的package路径再加函数名组成的。参数中,我们也只需要关心在JAVA程序中存在的参数,至于JNIEnv*和jclass我们一般没有必要去碰它。

好,下面我们用testdll.cpp文件具体实现这两个函数:

#include "testdll.h"

int i = 0;

JNIEXPORT jint JNICALL Java_testdll_get (JNIEnv *, jclass)

{

return i;

}

JNIEXPORT void JNICALL Java_testdll_set (JNIEnv *, jclass, jint j)

{

i = j;

}

编译连接成库文件,本例是在WINDOWS下做的,生成的是DLL文件。并且名称要与JAVA中需要调用的一致,这里就是goodluck.dll

把goodluck.dll拷贝到testdll.class的目录下,java testdll运行它,就可以观察到结果了。


转自:http://buttom2008.blog.163.com/blog/static/71449844200837511649/