2012-11-27 28 views
-1

首先我是新手而不是英文好的
是否可以將system.out.print放入框架中?讓我們說,如果我想看到所有我的輸出打印在文本字段或類似的東西。我可以把輸出打印在文本字段中,但它只是settext一個過程。我希望我的輸出在文本字段中的所有日誌,而不僅僅是一個輸出。
對不起,如果假的問題,謝謝您的回答之前
我有代碼,我希望把我所有的輸出文本2(文本框)把system.out.print放在框架中java [netbeans]

this is my whole code: 



import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JTextPane; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.Document; 
import org.jpos.iso.BaseChannel; 
import org.jpos.iso.ISOException; 
import org.jpos.iso.ISOMsg; 
import org.jpos.iso.ISOPackager; 
import org.jpos.iso.ISORequestListener; 
import org.jpos.iso.ISOServer; 
import org.jpos.iso.ISOSource; 
import org.jpos.iso.ServerChannel; 
import org.jpos.iso.channel.ASCIIChannel; 
import org.jpos.iso.packager.GenericPackager; 
import jpos.JPosServer; 

public class server extends javax.swing.JFrame { 


    public server() { 
    initComponents(); 
} 


    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

    mulai = new javax.swing.JButton(); 
    text = new javax.swing.JScrollPane(); 
    text1 = new javax.swing.JTextArea(); 
    jScrollPane1 = new javax.swing.JScrollPane(); 
    JTextPane = new javax.swing.JTextPane(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); 

    mulai.setText("Star Server"); 
    mulai.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      mulaiActionPerformed(evt); 
     } 
     }); 
     getContentPane().add(mulai, new org.netbeans.lib.awtextra.AbsoluteConstraints(129, 66, -1, -1)); 

     text1.setColumns(20); 
     text1.setRows(5); 
     text.setViewportView(text1); 

     getContentPane().add(text, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 96, 350, 130)); 

     jScrollPane1.setViewportView(JTextPane); 

     getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 250, 330, 170)); 

    pack(); 
}// </editor-fold> 




    private void log(String msg) { 
    JTextPane guiConsole = new JTextPane(); 
    Document doc = guiConsole.getDocument(); 
    try { 
     doc.insertString(doc.getLength(), msg + "\r\n", null); 
    } catch (BadLocationException e) {} 
    } 



    private void setText2Text(String msg) { 
    String toAppend = text1.getText(); 
    toAppend = toAppend + "/n" + msg; 
    text1.setText(toAppend); 
    } 

private void mulaiActionPerformed(java.awt.event.ActionEvent evt) { 
    // TODO add your handling code here: 

} 






    public boolean process1(ISOSource isoSrc, ISOMsg isoMsg) { 
    try { 
     log("Server menerima koneksi dari ["+((BaseChannel)isoSrc).getSocket().getInetAddress().getHostAddress()+"]"); 

     if (isoMsg.getMTI().equalsIgnoreCase("1800")) { 
       acceptNetworkMsg(isoSrc, isoMsg); 
     } 
    } catch (IOException ex) { 
     Logger.getLogger(JPosServer.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (ISOException ex) { 
     Logger.getLogger(JPosServer.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    return false; 
} 

    private void acceptNetworkMsg(ISOSource isoSrc, ISOMsg isoMsg) throws ISOException, IOException { 

     log("Accepting Network Management Request"); 

    ISOMsg reply = (ISOMsg) isoMsg.clone(); 
    reply.setMTI("1810"); 
    reply.set(39, "00"); 
    isoSrc.send(reply); 
} 


/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) throws ISOException { 

    String hostname = "localhost"; 
    int portNumber = 1234; 

    // membuat sebuah packager 
    ISOPackager packager = new GenericPackager("src/jpos/iso93ascii.xml"); 
    // membuat channel 
    ServerChannel channel = new ASCIIChannel(hostname, portNumber, packager); 
    // membuat server 
    ISOServer server = new ISOServer(portNumber, channel, null); 
    server.addISORequestListener(new JPosServer()); 
    new Thread(server).start(); 

    System.out.println("Server siap menerima koneksi pada port [" + portNumber+"]"); 


    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new server().setVisible(true); 
     } 
    }); 
} 
// Variables declaration - do not modify 
private javax.swing.JTextPane JTextPane; 
private javax.swing.JScrollPane jScrollPane1; 
private javax.swing.JButton mulai; 
private javax.swing.JScrollPane text; 
private javax.swing.JTextArea text1; 
// End of variables declaration 

}

感謝您對我的問題yogendra越來越,我真的很感激它

+0

如果要重定向用System.out.print()打印的輸出日誌,應該查看http://docs.oracle.com/javase/6/docs/api/java/lang/ System.html#setOut(java.io.PrintStream) – Pr0gr4mm3r

+0

正如我之前提到的那樣,我只是新手而且不擅長英語,我不明白它是什麼 –

回答

0

在每個text2.setText()之前,你可以先得到文本,然後在原始字符串中追加新的字符串。

String toAppend = text2.getText(); 
toAppend = toAppend + "/n" + "Your new string message here!"; 
text2.setText(toAppend); 

更新時間:

private void setText2Text(String msg) { 
    String toAppend = text2.getText(); 
    toAppend = toAppend + "/n" + msg; 
    text2.setText(toAppend); 
} 

然後在你的代碼,替代

text2.setText("Your message here") 

setText2Text("Your message here") 
+0

它沒有工作,它只是輸出第一個進程 –

+0

你將代碼添加到所有三種方法?或者爲了減少重複代碼,你可以編寫你自己的setTextMsg(String msg)方法 – Don

+0

我向所有三種方法添加代碼。那麼我減少它,因爲你提到之前,它仍然dosn't工作 –

0

不斷更新的text與附加文本使用setText方法本身。使用getText可檢索現有文本,並將新文本與現有文本以新行分隔。

使用System.lineSeparator()追加新線以下:

String text = text2.getText(); 
    text= text + System.lineSeparator() + "New line text";//<--put the text here 
    text2.setText(text); //<--update the text field value with modified text 

編輯:

要初始化的線程內的服務器。嘗試外實例化,並使用相同的設置文本:

final server myServer = new server(); 
/* Create and display the form */ 
java.awt.EventQueue.invokeLater(new Runnable() { 
    public void run() { 
     myServer.setVisible(true); 
    } 
    }); 

    JTextArea text1 = myServer.getText1(). 
    String text = text1.getText(); 
    text= text + System.lineSeparator() + "New line text";//<--put the text here 
    text1.setText(text); //<--update the text field value with modified text 

另外,在你的類定義text1一個getter方法:

public JTextArea getText1(){ 
    return this.text1; 
    } 
+0

謝謝你的回答yogendra但它沒有工作 –

+0

@poundPound:糟糕。我有一些錯別字。現在更正(更新了答案)。檢查並讓我知道,如果仍然不起作用。 –

+0

它的工作,但我可以settext靜態上下文? cek我的更新 –

0

來模擬你的GUI控制檯,您可能需要JTextPane,因爲JTextField只能顯示一行文字。您需要創建自己的方式「打印」到文本窗格(您不能使用System.out.println打印到GUI)。

代碼示例:

假設你有這樣的目標:

JTextPane guiConsole = new JTextPane(); 

您可以創建這樣一個功能:

private void log(String msg) { 
    Document doc = guiConsole.getDocument(); 
    try { 
     doc.insertString(doc.getLength(), msg + "\r\n", null); 
    } catch (BadLocationException e) {} 
} 

現在,只要你會使用System.out.println("Hello");打印到控制檯,只需撥打log("Hello");將相同的文本打印到您的文本窗格。

+0

謝謝你回答大衛,但它不顯示在textpane.where我聲明文本窗格參數假設變量名textpane是txtpane1? –

+0

@poundPound你的文本面板顯示?顯示gui後你有沒有調用過日誌方法? – davidXYZ

+0

你做錯了。 'JTextPane guiConsole = new JTextPane();'不應該在'log'方法中。它應該被聲明爲一個類字段。最好在剛剛聲明'jScrollPane1 = new javax.swing.JScrollPane();'之後的開頭。另外,不要忘記將它添加到你的框架,就像這個'getContentPane()。add(guiConsole)'。 – davidXYZ