lim1986 2011-10-09 14:06
浏览 340
已采纳

使用SWT的Text重定向System.out出现卡死现象,请问怎么解决呀?

[code="java"]
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class UI {
public static final String VERSION = "1.0";
private static final Display display = new Display();
private static final Shell shell = new Shell(display);
public static void main(String[] args) {
shell.setText("综合工具箱v"+VERSION);
shell.setSize(new Point(400,320));
shell.setLayout(new GridLayout(1, true));
initMail163();
createConsole();
center(shell);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
private static void initMail163() {
Composite content = new Composite(shell,SWT.BORDER);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
content.setLayoutData(gd);
content.setLayout(new RowLayout());
final Button mailOp = new Button(content, SWT.BORDER);
mailOp.setText("自动领积分");
mailOp.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
mailOp.setEnabled(false);
mockOutputToConsole();
mailOp.setEnabled(true);
}

        private void mockOutputToConsole() {
            while(true){
                System.out.println("test....");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
}

private static void center(Composite comp){
    Monitor primary = display.getPrimaryMonitor ();
    Rectangle bounds = primary.getBounds ();
    Rectangle rect = shell.getBounds ();
    int x = bounds.x + (bounds.width - rect.width) / 2;
    int y = bounds.y + (bounds.height - rect.height) / 2;
    comp.setLocation(x, y);
}

private static void createConsole(){
    Composite control = new Composite(shell,SWT.BORDER);
    GridData gd = new GridData(GridData.FILL_BOTH);
    control.setLayoutData(gd);
    control.setToolTipText("控制台");
    control.setSize(new Point(600,400));
    control.setLayout(new FillLayout());
    Text outputPanel = new Text(control, SWT.MULTI | SWT.V_SCROLL | SWT.WRAP );
    MyPrintStream mps = new MyPrintStream(System.out, outputPanel);
    System.setOut(mps);
    System.setErr(mps);
}

}

import java.io.OutputStream;
import java.io.PrintStream;
import org.eclipse.swt.widgets.Text;

import java.io.OutputStream;
import java.io.PrintStream;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;

public class MyPrintStream extends PrintStream{
private Text text;
public MyPrintStream(OutputStream out, Text text) {
super(out);
this.text = text;
}
/**
* 在这里重截,所有的打印方法都要调用的方法
*/
public void write(final byte[] buf, final int off, final int len) {
if(text != null && !text.isDisposed()){
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
text.append(new String(buf, off, len));
}
});
}else{
super.write(buf, off, len);
}
}
}
[/code]

  • 写回答

3条回答 默认 最新

  • easyroom 2011-10-09 15:02
    关注

    我把listener稍微改了下,你应该可以看出来为啥了。 你不[color=orange]能让主线程sleep[/color]啊
    [code="java"]
    private static void initMail163() {
    Composite content = new Composite(shell, SWT.BORDER);
    GridData gd = new GridData(GridData.FILL_HORIZONTAL);
    content.setLayoutData(gd);
    content.setLayout(new RowLayout());
    final Button mailOp = new Button(content, SWT.BORDER);
    mailOp.setText("自动领积分");
    mailOp.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {
    mailOp.setEnabled(false);
    mockOutputToConsole();
    mailOp.setEnabled(true);
    }

            private void mockOutputToConsole() {
                new Thread() {
                    public void run() {
                        while (true) {
                            System.out.println("test....");
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }.start();
    
            }
        });
    }
    

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!