读取文件到swt界面,界面拖拽不动,卡死的问题

甜甜小米粒 2011-11-18 04:07:43
写了一个简单的读取.sql文件内容到TEXT组件的小程序,文件内容能够读取到界面,但是SWT窗口不能拖动,一动就死掉了,哪位高手帮忙看一下:
package com.dba.ui;

import java.io.BufferedReader;
import java.io.FileReader;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class Main extends Shell {
private Text text;

/**
* Launch the application.
* @param args
*/
public static void main(String args[]) {
try {
Display display = Display.getDefault();
Main shell = new Main(display);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Create the shell.
* @param display
*/
public Main(Display display) {
super(display, SWT.SHELL_TRIM);

text = new Text(this, SWT.BORDER);
text.setBounds(10, 31, 422, 210);

Button button = new Button(this, SWT.NONE);
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
String lineMessage;
try {
BufferedReader bf = new BufferedReader(new FileReader("./script/Oracle.sql"));
while ((lineMessage = bf.readLine()) != null) {
text.append(lineMessage + "\n");
Thread.sleep(1000);
}

} catch (Exception e1) {
e1.printStackTrace();
}
}
});
button.setBounds(10, 3, 72, 22);
button.setText("\u8BFB\u53D6");
createContents();

}

/**
* Create contents of the shell.
*/
protected void createContents() {
setText("SWT Application");
setSize(450, 300);

}

@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}
...全文
190 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
甜甜小米粒 2011-11-22
  • 打赏
  • 举报
回复
非常感谢,我调试了一下,可以了
magong 2011-11-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sun_xhua 的回复:]

还有个问题要请教:是不是在SWT的syncExec方法中,只能执行对组件的操作?
[/Quote]
syncExec方法中尽量不要做延时大的动作。
甜甜小米粒 2011-11-21
  • 打赏
  • 举报
回复
厉害,呵呵,这样就没问题了。

还有个问题要请教:是不是在SWT的syncExec方法中,只能执行对组件的操作?

我写了一个从文件中读取SQL语句并自动执行SQL语句的小程序,边执行边往SWT界面中的Text组件里里打印SQL语句,java代码类似这样的:
if (message[0].equalsIgnoreCase("select")) {
text.append(getCurrentDate() + " " + lineMessage+ ".\n");
sqlsleep.sleep(1000);
st.executeQuery(lineMessage);
}
然后我在syncExec()方法中执行这个JAVA程序,SWT界面还是会卡死
magong 2011-11-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sun_xhua 的回复:]

谢谢magong,这样的话可以解决界面卡的问题,但是关闭SWT窗口,JAVA进程是关不上的
[/Quote]
好说。
将mouseDown方法进一步修改如下:

public void mouseDown(MouseEvent e) {
Thread t = new Thread(){
@Override
public void run(){
String lineMessage;
try {
BufferedReader bf = new BufferedReader(new FileReader(
"./script/Oracle.sql"));
while ((lineMessage = bf.readLine()) != null) {
final String aLine = lineMessage;
Display.getDefault().syncExec(new Runnable(){
@Override
public void run() {
text.append(aLine + "\n");
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

} catch (Exception e1) {
e1.printStackTrace();
}
}
};
t.setDaemon(true);
t.start();
}
甜甜小米粒 2011-11-21
  • 打赏
  • 举报
回复
谢谢magong,这样的话可以解决界面卡的问题,但是关闭SWT窗口,JAVA进程是关不上的
magong 2011-11-18
  • 打赏
  • 举报
回复
另外,为了能看出分行的效果,Text控件要这么新建:

text = new Text(this, SWT.MULTI | SWT.BORDER);
magong 2011-11-18
  • 打赏
  • 举报
回复
你的mouseDown方法要这么写才行:

public void mouseDown(MouseEvent e) {
new Thread(){
@Override
public void run(){
String lineMessage;
try {
BufferedReader bf = new BufferedReader(new FileReader(
"./script/Oracle.sql"));
while ((lineMessage = bf.readLine()) != null) {
final String aLine = lineMessage;
Display.getDefault().syncExec(new Runnable(){
@Override
public void run() {
text.append(aLine + "\n");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}

} catch (Exception e1) {
e1.printStackTrace();
}
}
}.start();
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧