轉帖|其它|編輯:郝浩|2010-09-28 10:45:49.000|閱讀 909 次
概述:本文主要介紹Java標準輸出重定向到GUI,希望對大家有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
實現輸出從控制臺到GUI并不復雜,只需要將標準輸出重定向。
重定向標準輸出很easy,System 類里有兩個靜態方法setErr(PrintStream err) 和 setOut(PrintStream out) 分別用于重定位“標準”錯誤輸出流和“標準”輸出流。只需要在程序初始時設置即可:
// GUIPrintStream guiPrintStream = new GUIPrintStream(System.out, jTextArea);
System.setErr(guiPrintStream);
System.setOut(guiPrintStream);
在上面的代碼中,我們發現一個新的類 GUIPrintStream,這是我們為 PrintStream 所做的包裝。因為我們的輸出目標位置是GUI,所以需要在 PrintStream 上做些文章,大家請看下面 GUIPrintStream 的代碼:
Java代碼
/**//*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.OutputStream;
import java.io.PrintStream;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;
/** *//**
* 輸出到文本組件的流。
*
* @author Chen Wei
* @website www.chenwei.mobi
* @email chenweionline@hotmail.com
*/
public class GUIPrintStream extends PrintStream...{
private JTextComponent component;
private StringBuffer sb = new StringBuffer();
public GUIPrintStream(OutputStream out, JTextComponent component)...{
super(out);
this.component = component;
}
/** *//**
* 重寫write()方法,將輸出信息填充到GUI組件。
* @param buf
* @param off
* @param len
*/
@Override
public void write(byte[] buf, int off, int len) ...{
final String message = new String(buf, off, len);
SwingUtilities.invokeLater(new Runnable()...{
public void run()...{
sb.append(message);
component.setText(sb.toString());
}
});
}
}
/**//*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.OutputStream;
import java.io.PrintStream;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;
/** *//**
* 輸出到文本組件的流。
*
* @author Chen Wei
* @website www.chenwei.mobi
* @email chenweionline@hotmail.com
*/
public class GUIPrintStream extends PrintStream...{
private JTextComponent component;
private StringBuffer sb = new StringBuffer();
public GUIPrintStream(OutputStream out, JTextComponent component)...{
super(out);
this.component = component;
}
/** *//**
* 重寫write()方法,將輸出信息填充到GUI組件。
* @param buf
* @param off
* @param len
*/
@Override
public void write(byte[] buf, int off, int len) ...{
final String message = new String(buf, off, len);
SwingUtilities.invokeLater(new Runnable()...{
public void run()...{
sb.append(message);
component.setText(sb.toString());
}
});
}
}
類 GUIPrintStream,繼承自 PrintStream 并且對它進行了一些修改。
GUIPrintStream 在構造函數中增加了一個 JTextComponent 變量,它就是我們的目標輸出 GUI 組件,它規定了目標輸出組件是一個文本組件。接下來覆寫了 write(byte[] buf, int off, int len)方法,這個方法原來的作用是將 len 字節從指定的初始偏移量為 off 的 byte 數組寫入此流,現在經過我們的修改,變成了將 byte 數組包裝成 String 寫入目標 GUI 組件。
簡單的代碼完成了將標準輸出重定向到 GUI 的全過程。由此延伸,還可以將標準輸出重定向到文本文件、從GUI獲取標準輸入等,就不一一介紹。
測試:
Java代碼
public class MainFrame extends javax.swing.JFrame {
public MainFrame() {
initComponents();
// 重定向到通過文本組件構建的組件輸出流中。
System.setOut(new GUIPrintStream(System.out, textArea));
}
private void initComponents() {
scrollPane = new javax.swing.JScrollPane();
textArea = new javax.swing.JTextArea();
btnOut = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("標準輸出重定向到GUI - www.chenwei.mobi");
textArea.setColumns(20);
textArea.setRows(5);
scrollPane.setViewportView(textArea);
getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER);
btnOut.setText("System.out.println(System.getProperties());");
btnOut.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnOutActionPerformed(evt);
}
});
getContentPane().add(btnOut, java.awt.BorderLayout.PAGE_END);
pack();
}
private void btnOutActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(System.getProperties());
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}
private javax.swing.JButton btnOut;
private javax.swing.JScrollPane scrollPane;
private javax.swing.JTextArea textArea;
}
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡轉載