写了一个简单的读取.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
}
}

解决方案 »

  1.   

    你的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();
    }
      

  2.   

    另外,为了能看出分行的效果,Text控件要这么新建:text = new Text(this, SWT.MULTI | SWT.BORDER);
      

  3.   

    谢谢magong,这样的话可以解决界面卡的问题,但是关闭SWT窗口,JAVA进程是关不上的
      

  4.   

    好说。
    将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();
    }
      

  5.   

    厉害,呵呵,这样就没问题了。还有个问题要请教:是不是在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界面还是会卡死
      

  6.   

    syncExec方法中尽量不要做延时大的动作。