作者showsky (踏实的活着)
看板java
标题Re: [问题] 让class和GUI沟通
时间Thu Oct 6 20:42:51 2011
※ 引述《x000032001 (某数..失业中)》之铭言:
: 我写了一个小程式
: 里面的文字出来的方式都是System.out.print
: 最近学了Swing
: 於是我又写了一个JFrame来让这只程式拥有GUI介面
: 但是问题来了
: 我左想右想也想不到
: 该怎麽让我本来的class里 藉由System浮出来的文字
: 变成在GUI里显示呢
: 应该把class包在GUI的建构子里面吗 这样的话又要怎麽让GUI跟class沟通呢
: 举例来说如果我想让Sysout的内容都能跑去JLabel
: 该怎麽写才对
: 请各位指点指点
override System.out.print ....
有两个指令可以用
public static setOut(PrintStream out)
public static setErr(PrintStream out)
class textAreaPrint extends OutputStream
{
private textArea;
public textAreaPrint()
{
this.textArea = new JTextArea();
}
@Override
public void write(byte[] b)
{
this.textArea.append(new String(b));
}
@Override
public void write(int b)
{
this.textArea.append(new String.valurOf(b));
}
@Override
public void write(byte[] b, int off, int len)
{
this.textArea.append(new String(b, off, len));
}
}
System.setOut(new PrintStream(new textAreaPrint()));
System.setErr(new PrintStream(new textAreaPrint()));
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 219.84.63.8