0

第一个我是新手,英语不好,
是否可以将 system.out.print 放入框架中?假设我想在文本字段中查看我的所有输出打印或类似的东西。我可以将输出打印放在文本字段中,但它只是 settext 一个进程。我想在文本字段中记录我的所有输出,而不仅仅是一个输出。
对不起,如果它是一个虚拟问题,谢谢你在
我有代码之前的回答,我想把我所有的输出放在 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,我非常感谢

4

3 回答 3

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");以将相同的文本打印到您的文本窗格。

于 2012-11-27T19:02:20.960 回答
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")
于 2012-11-27T17:54:33.237 回答
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;
  }
于 2012-11-27T18:01:13.570 回答