# ConsolenFenster in Applikation einbinden?



## Gast (21. Nov 2004)

Hallo,

ich möchte in meine Swing-Anwendung gerne eine Console einbinden. Wie kann ich das machen?

Danke für jeden Tip!

Tschau
Antje


----------



## Beni (21. Nov 2004)

Du kannst ein Textfeld in eine Console umfunktionieren.

Und dann leitest du System.out (in, err) um, indem du andere PrintStreams setzt (System.setOut, setIn, setErr)...


----------



## abollm (22. Nov 2004)

Hier zur *Anregung* eine auf Swing-Komponenten basierende Konsole:

```
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class Console extends JFrame {
	public final PipedInputStream in = new PipedInputStream();

	private PrintStream commandOut;

	private JTextPane displayPane;

	private JScrollPane displayPaneScroll;

	private JTextField inputField;

	private DefaultStyledDocument doc;

	// Prompt
	private String input = "> ";
	
	public Console() throws IOException {
		commandOut = new PrintStream(new PipedOutputStream(in));
		setTitle("Meine Konsole");
		setSize(500, 600);
		setLocation(100,100);

		Container myPane = getContentPane();

		doc = new DefaultStyledDocument();

		inputField = new JTextField();
		inputField.setText(input);
		inputField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				commandOut.println(e.getActionCommand());
				commandOut.flush();
				inputField.setText(input);
			}
		});
		displayPane = new JTextPane(doc);

		displayPaneScroll = new JScrollPane(displayPane,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

		displayPane.setBackground(Color.black);
		displayPane.setEditable(false);

		inputField.setBackground(Color.black);
		inputField.setForeground(Color.white);

		myPane.add(inputField, BorderLayout.SOUTH);
		myPane.add(displayPaneScroll, BorderLayout.CENTER);

		// Window Listener zum Schließen der Konsole
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

	}

	public void print(String text) {
		print(text, 255, 255, 255);
	}

	public void print(String text, int r, int g, int b) {
		SimpleAttributeSet attr = new SimpleAttributeSet();
		StyleConstants.setFontSize(attr, 12);
		StyleConstants.setForeground(attr, new Color(r, g, b));

		try {
			doc.insertString(doc.getLength(), text, attr);
		} catch (BadLocationException ex) {
		}
	}

	public void println(String text) {
		println(text, 255, 255, 255);
	}

	public void println(String text, int r, int g, int b) {
		SimpleAttributeSet attr = new SimpleAttributeSet();
		StyleConstants.setFontSize(attr, 12);
		StyleConstants.setForeground(attr, new Color(r, g, b));

		try {
			doc.insertString(doc.getLength(), text + "\n", attr);
		} catch (BadLocationException ex) {
		}
	}

	public void clearScreen() {
		displayPane.setText("");
	}

}
```

Folgendermaßen kannst du die Konsole aufrufen:


```
import javax.swing.*;
import java.io.*;

public class ConsoleTest {
	public static void main(String args[]) {
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {
		}

		ConsoleTest ct = new ConsoleTest();
		ct.start();
	}

	public void start() {
		try {
			Console console = new Console();
			console.setVisible(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					console.in));
			while (true) {
				console.println(in.readLine());
			}
		} catch (Exception e) {
		}

	}

}
```

Bedienug sollte intuitiv klar sein.
Vielleicht hilft es dir ja.


----------

