模拟eclipse控制台显示打印信息的程序

在这里实现了一个能模拟eclipse控制台显示打印信息的程序. 主要的技术点有下:

 

      1, 输出流重定向:

      将形如System.out.println();的输出内容重定向的原理:

 

     (1) System类的静态属性 out 的类型是 PrintStream; System.out.println即是调用PrintStream实例的println方法;

 

     (2) PrintStream 为其他输出流添加了功能,使它们能够方便地打印(列如打印到java控制台)各种数据值表示形式;

 

     (3) 我们可以自己书写 PrintStream 的子类, 将 子类PrintStream实例 作为参数传入System.setOut(PrintStream out) 方法中;

 

     (4) 这样,通过System.out调用打印方法输出到java控制台的内容将由自己定义的子类PrintStream进行处理;

 

     (5) 本例中是把输出流重定向到SWT的Text部件里面;如果仅是想把输出流重定向到文件,则可以这样:

 

            创建文件输出流: PrintStream out = new PrintStream("./txtFileName.txt"); 
            设置使用新的输出流: System.setOut(out);

 

      2, 在非UI线程(主线程) 访问UI的方式

      在非UI线程访问UI, 可以使用Display.getDefault().syncExec操作UI控件

 

 

 

     下面是程序运行中的界面:

 

 

      下面是程序代码 (为了节省篇幅, 去掉了import语句, 在eclipse中可用快捷键 ctrl + shift + o 导入org.eclipse.swt和javaIO相关的包):

Java代码   收藏代码
  1. public class MenuTest {  
  2.   
  3.     public static void main(String[] args) {  
  4.   
  5.         Display display = new Display();  
  6.         final Shell shell = new Shell(display);  
  7.         shell.setText("Console");  
  8.         shell.setBounds(100100500400);  
  9.         GridLayout gridLayout = new GridLayout();  
  10.         gridLayout.numColumns = 1;  
  11.         shell.setLayout(gridLayout);  
  12.   
  13.         //定义一个菜单,当点击File的Select Me Now选项时触发子线程中的循环打印  
  14.         Menu menuBar = new Menu(shell, SWT.BAR);  
  15.         shell.setMenuBar(menuBar);  
  16.   
  17.         MenuItem fileMenuItems = new MenuItem(menuBar, SWT.CASCADE);  
  18.         fileMenuItems.setText("&File");  
  19.         Menu subMenu = new Menu(shell, SWT.DROP_DOWN);  
  20.         fileMenuItems.setMenu(subMenu);  
  21.         MenuItem selectItem = new MenuItem(subMenu, SWT.NULL);  
  22.         selectItem.setText("&Select Me Now");  
  23.         selectItem.setAccelerator(SWT.CTRL + 'S');  
  24.         selectItem.addSelectionListener(new SelectionAdapter() {  
  25.             public void widgetSelected(SelectionEvent event) {  
  26.                 // 默认情况下监听事件时在主线程里执行  
  27.                 // 在子线程执行任务避免阻塞主线程  
  28.                 PrintThread printThread = new PrintThread("the First Item");  
  29.                 printThread.start();  
  30.             }  
  31.         });  
  32.         MenuItem sep = new MenuItem(subMenu, SWT.SEPARATOR);  
  33.         MenuItem exitItem = new MenuItem(subMenu, SWT.NULL);  
  34.         exitItem.setText("&Exit");  
  35.   
  36.         exitItem.addSelectionListener(new SelectionAdapter() {  
  37.             public void widgetSelected(SelectionEvent event) {  
  38.                 shell.dispose();  
  39.             }  
  40.         });  
  41.   
  42.         Text text = new Text(shell, SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);  
  43.         text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL  
  44.                 | GridData.FILL_VERTICAL));  
  45.   
  46.                 //输出重定向设置  
  47.         MyPrintStream mps = new MyPrintStream(System.out, text);  
  48.         System.setOut(mps);  
  49.         System.setErr(mps);  
  50.   
  51.         shell.open();  
  52.         while (!shell.isDisposed()) {  
  53.             if (!display.readAndDispatch())  
  54.                 display.sleep();  
  55.         }  
  56.         display.dispose();  
  57.         System.exit(0); // 杀掉子线程,终止jvm  
  58.     }  
  59.   
  60. }  
  61.   
  62. //定义一个PrintStream子类,将打印语句输出流重定向到Text组件中显示  
  63. class MyPrintStream extends PrintStream {  
  64.   
  65.     private Text text;  
  66.   
  67.     public MyPrintStream(OutputStream out, Text text) {  
  68.         super(out);  
  69.         this.text = text;  
  70.     }  
  71.     // 重写父类write方法,这个方法是所有打印方法里面都要调用的方法  
  72.     public void write(byte[] buf, int off, int len) {  
  73.         final String message = new String(buf, off, len);  
  74.   
  75.         // SWT非界面线程访问组件的方式  
  76.         Display.getDefault().syncExec(new Thread() {  
  77.             public void run() {  
  78.                 // 把信息添加到组件中  
  79.                 if (text != null && !text.isDisposed()) {  
  80.                     text.append(message);  
  81.                 }  
  82.             }  
  83.         });  
  84.     }  
  85.   
  86. }  
  87.   
  88. //在非UI线程中不断执行System.out.println()方法  
  89. class PrintThread extends Thread {  
  90.     private String name;  
  91.   
  92.     public PrintThread(String name) {  
  93.         this.name = name;  
  94.     }  
  95.     public void run() {  
  96.         while (true) {  
  97.             System.out.println(name + "was selected!");  
  98.         }  
  99.     }  
  100. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值