把system.out.print放在帧java [netbeans]中

时间:2012-11-27 17:48:07

标签: java netbeans

首先是新手,而且英语不太好 是否可以将system.out.print放入框架中?让我们说如果我想在textfield中看到我的所有输出打印或类似的东西。我可以在textfield中输出输出,但它只是settext一个进程。我希望我的输出的所有日志都在textfield中,而不仅仅是一个输出 对不起,如果是假问题,谢谢你的回答 我有代码,我想把我的所有输出放在text2(textfield)

   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,我真的很感激

3 个答案:

答案 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")

答案 1 :(得分: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;
  }

答案 2 :(得分: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");即可将相同的文字打印到文本窗格。