import java.awt.BorderLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField;public class Test extends JFrame { public static JTextArea text; //输出JTextArea private static class System //覆盖System.out.pringln() { private static class out {
private static void println(String a) { text.append(a);
} } }
public static void main(String args[]) {
try { Test frame = new Test(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } public Test() { super(); setBounds(100, 100, 500, 375); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); text = new JTextArea(); getContentPane().add(text, BorderLayout.CENTER);//测试按钮 final JButton button = new JButton(); button.addMouseListener(new MouseAdapter() { public void mouseClicked(final MouseEvent e) { System.out.println("OK"); } }); button.setText("New JButton"); getContentPane().add(button, BorderLayout.SOUTH); }}
主要就是这里了,覆盖一下System.out.println() 不过JTextArea必须声明为static的 我也在学习中,哪为大哥有更好的方法,还希望能学习学习public static JTextArea text; //输出JTextArea private static class System //覆盖System.out.pringln() { private static class out { private static void println(String a) { text.append(a); } } }
Kokonol的方法应该说很有创意,但截获在其他类里的输出就有问题了 标准的方法是调用 System.setOut(PrintStream out) System.setErr(PrintStream err) 即提供一个自定义的PrintStream,将一个JTextArea作为PrintStream的输出目的地。 基本思路(也可考虑使用PipedInputStream,PipedOutputStream ): OutputStream textAreaStream = new OutputStream() { public void write(int b) throws IOException { text.append(String.valueOf((char)b)); } public void write(byte b[]) throws IOException { text.append(new String(b)); }
public void write(byte b[], int off, int len) throws IOException { text.append(new String(b, off, len)); } }; PrintStream myOut = new PrintStream(textAreaStream); System.setOut(myOut); System.setErr(myOut);借用一下上面的示例代码: import java.awt.BorderLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField;import java.io.*;public class Test extends JFrame { public static JTextArea text; //输出JTextArea public static void main(String args[]) { OutputStream textAreaStream = new OutputStream() { public void write(int b) throws IOException { text.append(String.valueOf((char)b)); } public void write(byte b[]) throws IOException { text.append(new String(b)); }
public void write(byte b[], int off, int len) throws IOException { text.append(new String(b, off, len)); } }; PrintStream myOut = new PrintStream(textAreaStream); System.setOut(myOut); System.setErr(myOut);
try { Test frame = new Test (); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } public Test () { super(); setBounds(100, 100, 500, 375); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); text = new JTextArea(); getContentPane().add(text, BorderLayout.CENTER);//测试按钮 final JButton button = new JButton(); button.addMouseListener(new MouseAdapter() { public void mouseClicked(final MouseEvent e) { System.out.println("OK"); System.out.println("测试按钮"); } }); button.setText("New JButton"); getContentPane().add(button, BorderLayout.SOUTH); }}
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;public class Test extends JFrame { public static JTextArea text; //输出JTextArea
private static class System //覆盖System.out.pringln()
{
private static class out
{
private static void println(String a)
{
text.append(a);
}
}
}
public static void main(String args[]) {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} public Test() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); text = new JTextArea();
getContentPane().add(text, BorderLayout.CENTER);//测试按钮
final JButton button = new JButton();
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
System.out.println("OK");
}
});
button.setText("New JButton");
getContentPane().add(button, BorderLayout.SOUTH);
}}
不过JTextArea必须声明为static的
我也在学习中,哪为大哥有更好的方法,还希望能学习学习public static JTextArea text; //输出JTextArea
private static class System //覆盖System.out.pringln()
{
private static class out
{
private static void println(String a)
{
text.append(a);
}
}
}
标准的方法是调用
System.setOut(PrintStream out)
System.setErr(PrintStream err) 即提供一个自定义的PrintStream,将一个JTextArea作为PrintStream的输出目的地。
基本思路(也可考虑使用PipedInputStream,PipedOutputStream ):
OutputStream textAreaStream = new OutputStream() {
public void write(int b) throws IOException {
text.append(String.valueOf((char)b));
} public void write(byte b[]) throws IOException {
text.append(new String(b));
}
public void write(byte b[], int off, int len) throws IOException {
text.append(new String(b, off, len));
}
};
PrintStream myOut = new PrintStream(textAreaStream);
System.setOut(myOut);
System.setErr(myOut);借用一下上面的示例代码:
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;import java.io.*;public class Test extends JFrame {
public static JTextArea text; //输出JTextArea
public static void main(String args[]) { OutputStream textAreaStream = new OutputStream() {
public void write(int b) throws IOException {
text.append(String.valueOf((char)b));
} public void write(byte b[]) throws IOException {
text.append(new String(b));
}
public void write(byte b[], int off, int len) throws IOException {
text.append(new String(b, off, len));
}
};
PrintStream myOut = new PrintStream(textAreaStream);
System.setOut(myOut);
System.setErr(myOut);
try {
Test frame = new Test ();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} public Test () {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); text = new JTextArea();
getContentPane().add(text, BorderLayout.CENTER);//测试按钮
final JButton button = new JButton();
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
System.out.println("OK");
System.out.println("测试按钮");
}
});
button.setText("New JButton");
getContentPane().add(button, BorderLayout.SOUTH);
}}
这样也搞定了