JavaSwing中使用多线程实时重定向控制台消息到JTextArea。

如题,要实时重定向到JTextArea。第一步,先实现重定向这一功能,代码如下:

public class JTextAreaOutputStream extends OutputStream
{
    private final JTextArea destination;

    static private ExecutorService service = Executors.newCachedThreadPool(new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "output");
        }
    });


    public JTextAreaOutputStream (JTextArea destination)
    {
        if (destination == null)
            throw new IllegalArgumentException ("Destination is null");


        this.destination = destination;
    }
    @Override
    public void write(byte[] buffer, int offset, int length) throws IOException
    {
        final String text = new String (buffer, offset, length);
        SwingUtilities.invokeLater(new Runnable ()
        {
            @Override
            public void run()
            {
                    destination.append(text);
            }
        });
    }
    @Override
    public void write(int b) throws IOException
    {
        write (new byte [] {(byte)b}, 0, 1);
    }

第二步,在Swing界面对应位置创建上述实体对象,同时实现重定向功能:

JTextAreaOutputStream jto = new JTextAreaOutputStream(jTextArea);
System.setOut (new PrintStream(jto));//设置输出重定向
System.setErr(new PrintStream(jto));//将错误输出也重定向,用于e.pritnStackTraced

第三步,在实现重定向功能之后,在相同地方创建一个新的进程执行要执行的有输出的代码:

new Thread(new Runnable() {
    @Override
    public void run() {
        //原本要执行的代码。
    }
}).start();

如上,问题就已经解决了,如果不加入第三步,则不能实现实时更新JTextArea内容,同时Swing界面会卡死,一直到当前程序执行完毕之后,一次性输出所有内容,此时卡死才会结束。网上查的内容是单线程,当前Swing进程阻塞,用来执行我们要执行的代码,这个时候给其放入另一线程,则不会出现阻塞问题,解决问题。希望有用。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
如果你想在Java Swing应用程序使用控制台界面,可以使用Java的System类来实现。具体来说,你可以使用System类的标准输入(System.in)和标准输出(System.out)方法来读取和写入控制台输入和输出。 首先,你需要创建一个新的控制台窗口。可以通过创建一个新的JFrame对象并将其设置为可见来实现: ```java import javax.swing.JFrame; public class ConsoleWindow { public static void main(String[] args) { JFrame consoleFrame = new JFrame("Console Window"); consoleFrame.setSize(400, 400); consoleFrame.setVisible(true); } } ``` 然后,你需要将标准输入和输出重定向控制台窗口。可以使用System.setIn()和System.setOut()方法来实现。例如: ```java import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; import javax.swing.JFrame; import javax.swing.JTextArea; public class ConsoleWindow { public static void main(String[] args) throws IOException { JFrame consoleFrame = new JFrame("Console Window"); consoleFrame.setSize(400, 400); JTextArea consoleArea = new JTextArea(); consoleFrame.add(consoleArea); // Create pipes for input and output streams PipedInputStream inPipe = new PipedInputStream(); PipedOutputStream outPipe = new PipedOutputStream(); // Connect the pipes to the console input and output streams System.setIn(inPipe); System.setOut(new PrintStream(outPipe)); // Start a new thread to read from the input pipe and write to the console new Thread(() -> { byte[] buffer = new byte[1024]; int bytesRead; try { while ((bytesRead = inPipe.read(buffer)) != -1) { consoleArea.append(new String(buffer, 0, bytesRead)); } } catch (IOException ex) { ex.printStackTrace(); } }).start(); consoleFrame.setVisible(true); } } ``` 这个例子创建了一个新的JTextArea对象并将其添加到JFrame窗口。然后,它创建了一个PipedInputStream对象和一个PipedOutputStream对象,并将它们连接到标准输入和输出流。接下来,它启动了一个新的线程来读取输入管道并将其写入JTextArea对象。 现在,当你运行Java应用程序时,它将在控制台窗口显示。你可以使用System.out.println()方法来输出文本,并使用Scanner类来读取文本输入。例如: ```java import java.util.Scanner; public class ConsoleOutput { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter your name:"); String name = scanner.nextLine(); System.out.println("Hello, " + name + "!"); } } ``` 这个例子要求用户输入他们的名字,然后将其打印到控制台窗口

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值