java gui 控制台_在GUI面板中创建Java控制台
這是一個功能強大的課程。您可以將此實例安裝到系統中并使用以下錯誤:
PrintStream con=new PrintStream(new TextAreaOutputStream(...));
System.setOut(con);
System.setErr(con);
更新時間2014-02-19:使用EventQueue.invokeLater()來避免GUI線程問題,這些問題很少會與原始問題一起出現。
更新2014-02-27:更好的實施
更新2014-03-25:正確記錄和刪除文本區域中的行,run()以避免在附加和刪除之間出現競爭條件,如果控制臺充滿輸出,則可能發生這種情況。對我來說,最終結果似乎更清晰。
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class TextAreaOutputStream
extends OutputStream
{
// *************************************************************************************************
// INSTANCE MEMBERS
// *************************************************************************************************
private byte[]? ? ? ? ? ? ? ? ? ? ? ? ? oneByte;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // array for write(int val);
private Appender? ? ? ? ? ? ? ? ? ? ? ? appender;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// most recent action
public TextAreaOutputStream(JTextArea txtara) {
this(txtara,1000);
}
public TextAreaOutputStream(JTextArea txtara, int maxlin) {
if(maxlin<1) { throw new IllegalArgumentException("TextAreaOutputStream maximum lines must be positive (value="+maxlin+")"); }
oneByte=new byte[1];
appender=new Appender(txtara,maxlin);
}
/** Clear the current console text area. */
public synchronized void clear() {
if(appender!=null) { appender.clear(); }
}
public synchronized void close() {
appender=null;
}
public synchronized void flush() {
}
public synchronized void write(int val) {
oneByte[0]=(byte)val;
write(oneByte,0,1);
}
public synchronized void write(byte[] ba) {
write(ba,0,ba.length);
}
public synchronized void write(byte[] ba,int str,int len) {
if(appender!=null) { appender.append(bytesToString(ba,str,len)); }
}
@edu.umd.cs.findbugs.annotations.SuppressWarnings("DM_DEFAULT_ENCODING")
static private String bytesToString(byte[] ba, int str, int len) {
try { return new String(ba,str,len,"UTF-8"); } catch(UnsupportedEncodingException thr) { return new String(ba,str,len); } // all JVMs are required to support UTF-8
}
// *************************************************************************************************
// STATIC MEMBERS
// *************************************************************************************************
static class Appender
implements Runnable
{
private final JTextArea? ? ? ? ? ? ?textArea;
private final int? ? ? ? ? ? ? ? ? ?maxLines;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// maximum lines allowed in text area
private final LinkedList? ?lengths;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // length of lines within text area
private final List? ? ? ? ? values;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// values waiting to be appended
private int? ? ? ? ? ? ? ? ? ? ? ? ?curLength;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // length of current line
private boolean? ? ? ? ? ? ? ? ? ? ?clear;
private boolean? ? ? ? ? ? ? ? ? ? ?queue;
Appender(JTextArea txtara, int maxlin) {
textArea =txtara;
maxLines =maxlin;
lengths? =new LinkedList();
values? ?=new ArrayList();
curLength=0;
clear? ? =false;
queue? ? =true;
}
synchronized void append(String val) {
values.add(val);
if(queue) { queue=false; EventQueue.invokeLater(this); }
}
synchronized void clear() {
clear=true;
curLength=0;
lengths.clear();
values.clear();
if(queue) { queue=false; EventQueue.invokeLater(this); }
}
// MUST BE THE ONLY METHOD THAT TOUCHES textArea!
public synchronized void run() {
if(clear) { textArea.setText(""); }
for(String val: values) {
curLength+=val.length();
if(val.endsWith(EOL1) || val.endsWith(EOL2)) {
if(lengths.size()>=maxLines) { textArea.replaceRange("",0,lengths.removeFirst()); }
lengths.addLast(curLength);
curLength=0;
}
textArea.append(val);
}
values.clear();
clear =false;
queue =true;
}
static private final String? ? ? ? ?EOL1="\n";
static private final String? ? ? ? ?EOL2=System.getProperty("line.separator",EOL1);
}
} /* END PUBLIC CLASS */
總結
以上是生活随笔為你收集整理的java gui 控制台_在GUI面板中创建Java控制台的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql+地图网格数据下载_echar
- 下一篇: java mp4 视频时间戳_MP4文件