PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : [Java] Simpler GUI-Ersatz für System.out.println?


Undertaker
2010-06-20, 16:09:14
Hallo,

ich möchte zum ersten Mal ein Programm aus meiner Entwicklungsumgebung BlueJ auskapseln und eigenständig ausführbar machen. Problem ist dabei nur, dass bisherigen Ausgaben per System.out.println zunächst nicht ohne weiteres funktionieren. Behelfsmäßig starte ich daher meine .jar Datei per Batch-File ("java -jar xyz.jar"), um so zumindest eine Ausgabe in der Windows-Konsole zu erhalten - klappt soweit auch ganz gut. Ist natürlich keine besonders schöne Lösung, darum meine Frage:

Gibt es eine simple Möglichkeit, eine Ausgabe direkt in einem billigen GUI-Fenster anzeigen zu lassen?

Besten Dank! :)

Ganon
2010-06-20, 16:36:10
Das hier ist jetzt weder besonders schön, noch elegant, aber ich hab mal fix was zusammengehackt:


package logtest;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

class GUIPrintStream extends PrintStream {

private boolean errorStream;
private JFrame frame;
private JTextArea area;

public GUIPrintStream(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
super(file, csn);
}

public GUIPrintStream(File file) throws FileNotFoundException {
super(file);
}

public GUIPrintStream(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
super(fileName, csn);
}

public GUIPrintStream(String fileName) throws FileNotFoundException {
super(fileName);
}

public GUIPrintStream(OutputStream out, boolean autoFlush, String encoding) throws UnsupportedEncodingException {
super(out, autoFlush, encoding);
}

public GUIPrintStream(OutputStream out, boolean autoFlush) {
super(out, autoFlush);
}

public GUIPrintStream(OutputStream out) {
super(out);
}

public boolean isErrorStream() {
return errorStream;
}

public void setErrorStream(boolean errorStream) {
this.errorStream = errorStream;
}

@Override
public void println(boolean x) {
// super.println(x);
Object obj = String.valueOf(x);
log(obj);
}

@Override
public void println(char x) {
// super.println(x);
Object obj = String.valueOf(x);
log(obj);
}

@Override
public void println(int x) {
// super.println(x);
Object obj = String.valueOf(x);
log(obj);
}

@Override
public void println(long x) {
// super.println(x);
Object obj = String.valueOf(x);
log(obj);
}

@Override
public void println(float x) {
// super.println(x);
Object obj = String.valueOf(x);
log(obj);
}

@Override
public void println(double x) {
// super.println(x);
Object obj = String.valueOf(x);
log(obj);
}

@Override
public void println(char[] x) {
// super.println(x);
Object obj = String.valueOf(x);
log(obj);
}

@Override
public void println(String x) {
// super.println(x);
Object obj = String.valueOf(x);
log(obj);
}

@Override
public void println(Object x) {
// super.println(x);
log(x);
}

private void initFrame() {
if (this.frame == null) {
this.frame = new JFrame();
this.frame.setLayout(new BorderLayout());
this.area = new JTextArea("Log ready");
this.area.setLineWrap(true);
this.area.setWrapStyleWord(true);
JScrollPane jsp = new JScrollPane(this.area);
this.frame.add(jsp, BorderLayout.CENTER);
this.frame.setSize(640, 480);
this.frame.setVisible(true);
}
}

private void log(final Object obj) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
initFrame();
String txt = GUIPrintStream.this.area.getText()+"\n";
txt += String.valueOf(obj);
GUIPrintStream.this.area.setText(txt);
}
});
}
}

public class Main {

/**
* @param args the command line arguments
*/
public static void setupLogging() {
PrintStream stderrStream = System.err;
GUIPrintStream newStderrStream = new GUIPrintStream(stderrStream);
newStderrStream.setErrorStream(true);
System.setErr(newStderrStream);

PrintStream stdoutStream = System.out;
GUIPrintStream newStdoutStream = new GUIPrintStream(stdoutStream);
newStdoutStream.setErrorStream(false);
System.setOut(newStdoutStream);
}

public static void main(String[] args) {
setupLogging();

System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
}
}


Das Ganze geht natürlich so erst mal nur für println. Aber es hindert einen keiner dran auch print etc. zu überschreiben. Fensterhandling könnte man auch noch hinzufügen. Auch sollte man bedenken das Log auch irgendwann mal zu leeren, sonst wird das immer langsamer und langsamer.

Undertaker
2010-06-20, 17:05:15
Hui, dass ist ja doppelt so viel Quelltext wie mein eigentliches Programm. :D Aber OK, wenn das schon die einfache Lösung ist... Besten Dank, ich werds mal testen. ;)

Ganon
2010-06-20, 17:26:49
Naja, das meiste im Code da ist das überschreiben der Methoden. Wirklich "interessant" ist da nur der Part, denn den Rest hat quasi die IDE erstellt:


private void initFrame() {
if (this.frame == null) {
this.frame = new JFrame();
this.frame.setLayout(new BorderLayout());
this.area = new JTextArea("Log ready");
this.area.setLineWrap(true);
this.area.setWrapStyleWord(true);
JScrollPane jsp = new JScrollPane(this.area);
this.frame.add(jsp, BorderLayout.CENTER);
this.frame.setSize(640, 480);
this.frame.setVisible(true);
}
}

private void log(final Object obj) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
initFrame();
String txt = GUIPrintStream.this.area.getText()+"\n";
txt += String.valueOf(obj);
GUIPrintStream.this.area.setText(txt);
}
});
}

public static void setupLogging() {
PrintStream stderrStream = System.err;
GUIPrintStream newStderrStream = new GUIPrintStream(stderrStream);
newStderrStream.setErrorStream(true);
System.setErr(newStderrStream);

PrintStream stdoutStream = System.out;
GUIPrintStream newStdoutStream = new GUIPrintStream(stdoutStream);
newStdoutStream.setErrorStream(false);
System.setOut(newStdoutStream);
}


Sieht doch schon deutlich weniger aus, aber insgesamt funktionieren tut nur der Code oben ^_^.

Wenn man später größere Programme schreibt, die man auch an andere verteilen möchte, dann wird Logging sowieso noch mal ein extra Thema.