简体   繁体   English

即使发生其他操作,在jtextarea中显示控制台输出

[英]Displaying console output in jtextarea even when other actions occur

I found this code here: how to display console output in java JTextarea one by one in a loop when button action is triggered and it displays console output in a jtextarea. 我在这里找到了以下代码: 触发按钮操作时如何在循环显示中以Java JTextarea的方式逐一显示控制台输出,并且在jtextarea中显示控制台输出。 I have added this class as action in a jmenuitem. 我已经将此类作为动作添加到jmenuitem中。 So that it appears when I want and when I run other classes it will show the output there. 这样它就会在我需要时显示,并且在我运行其他类时将在此处显示输出。 However when I launch it, it works properly, but if I try and launch another class which will show output in console and accept userinput, the jtextarea which is supposed to show console output at the same time, it freezes. 但是,当我启动它时,它可以正常工作,但是,如果我尝试启动另一个将在控制台中显示输出并接受用户输入的类,则应该同时显示控制台输出的jtextarea会冻结。 How could I make it so that it stays tuned, despite invoking other classes/frames? 尽管调用了其他类/框架,我如何才能使其保持调谐状态? Thanks in advance 提前致谢

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DynamicWrite implements ActionListener
{
    JFrame frame = new JFrame("TextArea");
    JTextArea tArea = new JTextArea(10,20);
    JButton button = new JButton("Click");
    JScrollPane pane = new JScrollPane(tArea);
    SwingWorker worker;
    String s= "Java is an Object Oriented Programming langauge...Java is static typed language...asbfldfjsdj";//some random String
    public void prepareAndShowGUI()
    {
        Container container = frame.getContentPane();
        container.add(pane);container.add(button,BorderLayout.NORTH);
        tArea.setLineWrap(true);
        tArea.setWrapStyleWord(true) ;
        button.addActionListener(this);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public void actionPerformed(ActionEvent evt)
    {
        if(evt.getSource()==button)
        {
            tArea.setText("");
            if (worker!=null)
            {
                worker.cancel(true);
            }
            worker = new SwingWorker()
            {
                @Override
                protected Integer doInBackground()//Perform the required GUI update here.
                {
                    try
                    {
                        for(int i = 0;i<s.length();i++)
                        {
                            tArea.append(String.valueOf(s.charAt(i)));
                            Thread.sleep(5);
                        }
                    }catch(Exception ex){}
                    return 0;
                }       
            };
            worker.execute();//Schedules this SwingWorker for execution on a worker thread.
        }
    }   
    public static void main(String st[])
    {
        DynamicWrite dyna = new DynamicWrite();
        dyna.prepareAndShowGUI();
    }
}

The example you have (apart from from being a bad example as outlined by Hovercraft Full of Eels) has nothing to do with redirecting console output. 您拥有的示例(除了像《 Hovercraft Full of Eels》所描述的那样是一个不良示例之外)与重定向控制台输出无关。

If you want to redirect the standard out to the text area, take a look at How to set output stream to TextArea for an example 如果要将标准重定向到文本区域,请查看如何将输出流设置为TextArea作为示例。

If you want to redirect the output of some other process, then you can only do this if you've launched the process yourself, there's no (easy way that I know of) to connect to the standard out/in of another running process (that your program didn't start itself directly) 如果您想重定向其他流程的输出,那么只有在您自己启动流程后才能执行此操作,没有(我知道的简便方法)可以连接到另一个正在运行的流程的标准输出(您的程序没有直接启动本身)

Check out Printing a Java InputStream from a Process for an example 查看示例中的从流程中打印Java InputStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM