Re: [問題] 讓class和GUI溝通
※ 引述《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):