![]() ![]() |
Jun 21 2004, 01:58 PM
Post
#1
|
|
|
Newbie ![]() Group: Members Posts: 2 Joined: 21-June 04 Member No.: 1924 |
จะสามารถเขียน Applet ที่ทำหน้าที่เหมือน Java Console สามารถแสดงผลด้วยคำสั่ง System.out.println("XXXXXXXX"); ได้ไหมคะ
|
|
|
|
Jun 21 2004, 04:25 PM
Post
#2
|
|
![]() Topgun Group: Topgun Posts: 3158 Joined: 5-February 04 From: On your monitor Member No.: 1281 |
ถ้าทำงานคล้าย ๆ กับระบบ Console คุณ ktoth ต้องจัดการกับ Stream ของคลาส System ครับ คือ System.out และอาจจะแถมไปที่ System.err ด้วยก็ได้
โดยใช้ Statis Method System.setOut(PrintStream out) System.setErr(PrintStream err) จากนั้นก็สร้างคลาสสักตัวที่ extends OutputStream แล้วทำการ override method ที่ชื่อว่า write(int b) แล้วก็ System.setOut(new PrintStream(new MyOutputStream()) PrintStream เดิมของคลาส System จะเป็น Native Method ส่งข้อความไปยัง Console คุณก็เขียนให้ write(int b) วาดรูปขึ้นบน Applet แทน ... วิธีนี้น่าจะช่วยได้นะครับ This post has been edited by iWat: Jun 21 2004, 07:56 PM |
|
|
|
Jun 21 2004, 07:56 PM
Post
#3
|
|
![]() Topgun Group: Topgun Posts: 3158 Joined: 5-February 04 From: On your monitor Member No.: 1281 |
ลองศึกษา code นี้ดูนะครับ เชื่อว่าคงช่วยได้บ้าง
CODE import javax.swing.*; import java.io.*; import java.awt.*; import java.awt.event.*; public class HelloWorld extends JApplet { private StringBuffer data = new StringBuffer(); public void init() { PrintStream p = new PrintStream(new AppletOutputStream(this)); System.setOut(p); JButton but = new JButton("Test"); but.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Hello World"); } }); this.setLayout(new FlowLayout()); this.add(but); } public void printchar(int b) { data.append((char) b); repaint(); } public void paint(Graphics g) { g.drawString(data.toString(), 50, 100); } } CODE import java.io.*;
public class AppletOutputStream extends OutputStream { private HelloWorld a; public AppletOutputStream(HelloWorld a) { this.a = a; } public void write(int b) { a.printchar(b); } } |
|
|
|
Jun 22 2004, 04:05 PM
Post
#4
|
|
|
Newbie ![]() Group: Members Posts: 2 Joined: 21-June 04 Member No.: 1924 |
พี่เก่งจัง |
|
|
|
Dec 22 2007, 06:10 PM
Post
#5
|
|
|
Member ![]() ![]() Group: Members Posts: 210 Joined: 21-May 05 Member No.: 3669 |
ผมลองใช้
CODE PrintStream myPrintStream = new PrintStream(new myOutput()); System.setOut(myPrintStream); System.setErr(myPrintStream); ... class myOutput extends OutputStream { public void write(int c) { char a[] = new char[1]; a[0] = (char)c; myProgram.this.myTextArea.append(new String(s)); } จะเจอ warning ว่า deprecated API ตรง new PrintStream ครับ -จะแก้ warning ใช้คำสั่งอะไรดี -และก็มีวิธี append int เป็น char ลง AWT TextArea โดยตรงมั้ยครับ ขอบคุณครับ This post has been edited by bpitk: Dec 22 2007, 06:11 PM |
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: 9th February 2010 - 09:14 AM |