轉(zhuǎn)帖|其它|編輯:郝浩|2010-09-28 10:45:49.000|閱讀 909 次
概述:本文主要介紹Java標(biāo)準(zhǔn)輸出重定向到GUI,希望對(duì)大家有幫助。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
實(shí)現(xiàn)輸出從控制臺(tái)到GUI并不復(fù)雜,只需要將標(biāo)準(zhǔn)輸出重定向。
重定向標(biāo)準(zhǔn)輸出很easy,System 類里有兩個(gè)靜態(tài)方法setErr(PrintStream err) 和 setOut(PrintStream out) 分別用于重定位“標(biāo)準(zhǔn)”錯(cuò)誤輸出流和“標(biāo)準(zhǔn)”輸出流。只需要在程序初始時(shí)設(shè)置即可:
// GUIPrintStream guiPrintStream = new GUIPrintStream(System.out, jTextArea);
System.setErr(guiPrintStream);
System.setOut(guiPrintStream);
在上面的代碼中,我們發(fā)現(xiàn)一個(gè)新的類 GUIPrintStream,這是我們?yōu)?PrintStream 所做的包裝。因?yàn)槲覀?的輸出目標(biāo)位置是GUI,所以需要在 PrintStream 上做些文章,大家請(qǐng)看下面 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 并且對(duì)它進(jìn)行了一些修改。
GUIPrintStream 在構(gòu)造函數(shù)中增加了一個(gè) JTextComponent 變量,它就是我們的目標(biāo)輸出 GUI 組件,它規(guī)定了目標(biāo)輸出組件是一個(gè)文本組件。接下來覆寫了 write(byte[] buf, int off, int len)方法,這個(gè)方法原來的作用是將 len 字節(jié)從指定的初始偏移量為 off 的 byte 數(shù)組寫入此流,現(xiàn)在經(jīng)過我們的修改,變成了將 byte 數(shù)組包裝成 String 寫入目標(biāo) GUI 組件。
簡(jiǎn)單的代碼完成了將標(biāo)準(zhǔn)輸出重定向到 GUI 的全過程。由此延伸,還可以將標(biāo)準(zhǔn)輸出重定向到文本文件、從GUI獲取標(biāo)準(zhǔn)輸入等,就不一一介紹。
測(cè)試:
Java代碼
public class MainFrame extends javax.swing.JFrame {
public MainFrame() {
initComponents();
// 重定向到通過文本組件構(gòu)建的組件輸出流中。
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("標(biāo)準(zhǔn)輸出重定向到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;
}
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載