2012-12-15 54 views
4

我想將sytem.out.println重定向到另一個類中的JLabel。Java - 將system.out.println重定向到JLabel

我有2個類,NextPage和Mctrainer。

NextPage基本上只是一個Jframe(我的項目的GUI),並且我在Nextpage中使用此代碼創建了一個Jlabel;

public class NextPage extends JFrame { 

    JLabel label1; 

    NextPage() { 
     label1 = new JLabel(); 
     label1.setText("welcome"); 
     getContentPane().add(label1); 

這是Mctrainer的代碼:

public class Mctrainer { 

    JLabel label1; 

    Mctrainer() { 
     HttpClient client2 = new DefaultHttpClient(); 
     HttpPost post = new HttpPost("http://oo.hive.no/vlnch"); 
     HttpProtocolParams.setUserAgent(client2.getParams(),"android"); 
     try { 
      List <NameValuePair> nvp = new ArrayList <NameValuePair>(); 
      nvp.add(new BasicNameValuePair("username", "test")); 
      nvp.add(new BasicNameValuePair("password", "test")); 
      nvp.add(new BasicNameValuePair("request", "login")); 
      nvp.add(new BasicNameValuePair("request", "mctrainer")); 
      post.setEntity(new UrlEncodedFormEntity(nvp)); 

      HttpContext httpContext = new BasicHttpContext(); 

      HttpResponse response1 = client2.execute(post, httpContext); 
      BufferedReader rd = new BufferedReader(new InputStreamReader(response1.getEntity().getContent())); 
      String line = ""; 
      while ((line = rd.readLine()) != null) { 
       System.out.println(line); 
      } 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

Mctrainer基本上只是從使用System.out.println服務器打印出JSON數據。 我想重定向它在我的GUI(NextPage)而不是控制檯的JLabel中顯示。 關於如何做到這一點的任何建議?

+1

*「in other class。」* Wh每當我看到那個短語時,它讓我懷疑提問者是否對如何用OO語言編程有任何想法。在開始編寫GUI的階段,它應該是一個非問題。如果這是一個問題,請停止嘗試對GUI進行一段時間的編程,並返回一些修訂的基礎知識。 –

+0

我只是認爲它會提及它以防萬一它從system.out.println重定向。 – user1880046

+0

這個問題是類似於:http://stackoverflow.com/questions/12945537/how-to-set-output-stream-to-textarea –

回答

7

你只需要改變默認的輸出...

退房System.setOut(printStream)

public static void main(String[] args) throws UnsupportedEncodingException 
{ 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    System.setOut(new PrintStream(bos)); 
    System.out.println("outputing an example"); 
    JOptionPane.showMessageDialog(null, "Captured: " + bos.toString("UTF-8")); 
} 

而且,你的問題是非常相似this other one,所以我可以適應this accepted answer一起工作JLabel

public static void main(String[] args) throws UnsupportedEncodingException 
{ 
    CapturePane capturePane = new CapturePane(); 
    System.setOut(new PrintStream(new StreamCapturer("STDOUT", capturePane, System.out))); 

    System.out.println("Output test"); 

    JFrame frame = new JFrame(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLayout(new BorderLayout()); 
    frame.add(capturePane); 
    frame.setSize(200, 200); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 

    System.out.println("More output test"); 
} 

public static class CapturePane extends JPanel implements Consumer { 

    private JLabel output; 

    public CapturePane() { 
     setLayout(new BorderLayout()); 
     output = new JLabel("<html>"); 
     add(new JScrollPane(output)); 
    } 

    @Override 
    public void appendText(final String text) { 
     if (EventQueue.isDispatchThread()) { 
      output.setText(output.getText() + text + "<br>"); 
     } else { 

      EventQueue.invokeLater(new Runnable() { 
       @Override 
       public void run() { 
        appendText(text); 
       } 
      }); 

     } 
    }   
} 

public interface Consumer {   
    public void appendText(String text);   
} 


public static class StreamCapturer extends OutputStream { 

    private StringBuilder buffer; 
    private String prefix; 
    private Consumer consumer; 
    private PrintStream old; 

    public StreamCapturer(String prefix, Consumer consumer, PrintStream old) { 
     this.prefix = prefix; 
     buffer = new StringBuilder(128); 
     buffer.append("[").append(prefix).append("] "); 
     this.old = old; 
     this.consumer = consumer; 
    } 

    @Override 
    public void write(int b) throws IOException { 
     char c = (char) b; 
     String value = Character.toString(c); 
     buffer.append(value); 
     if (value.equals("\n")) { 
      consumer.appendText(buffer.toString()); 
      buffer.delete(0, buffer.length()); 
      buffer.append("[").append(prefix).append("] "); 
     } 
     old.print(c); 
    }   
} 
+0

重新塗漆怎麼樣? – Zhedar

+0

@José 有沒有機會給我一個例子?我花了最後4個小時試圖重定向它,至今沒有任何運氣。 – user1880046

+1

下面是一個例子:http://viralpatel.net/blogs/java-reassign-standard-output-error/ –