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 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题