java - 在外部 jframe 中显示 java 控制台

标签 java console jframe

我制作了一个java应用程序,但现在我需要在外部jframe中显示控制台的所有输出(如异常、println、ecc...)。 我查看了一些解决方案,但我不知道我需要做什么。

请不要将其标记为重复,因为我找不到任何东西可以解决我的问题。

我尝试过这个:

public class Main extends JFrame {
    public static void main(String[] args) {
        Console console = new Console();
        console.init();
        Main launcher = new Main();
        launcher.setVisible(true);
        console.getFrame().setLocation(
                launcher.getX() + launcher.getWidth() + launcher.getInsets().right,
                launcher.getY());
    }

    private Main() {
        super();
        setSize(600, 600);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

class Console {
    final JFrame frame = new JFrame();

    public Console() {
        JTextArea textArea = new JTextArea(24, 80);
        textArea.setBackground(Color.BLACK);
        textArea.setForeground(Color.LIGHT_GRAY);
        textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
        System.setOut(new PrintStream(new OutputStream() {
            @Override
            public void write(int b) throws IOException {
                textArea.append(String.valueOf((char) b));
            }
        }));
        frame.add(textArea);
    }

    public void init() {
        frame.pack();
        frame.setVisible(true);
        System.out.println("Hello world!");
    }

    public JFrame getFrame() {
        return frame;
    }
}

但它不起作用。

我应该做什么?谢谢。

最佳答案

如果您只需要在控制台上显示字符串,这将起作用。也许您需要先将更复杂的数据转换为字符串。

package sof2;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class Main2 extends JFrame {
  public static void main(String[] args) {
    Console console = new Console();
    console.init();
    Main2 launcher = new Main2();
    launcher.setVisible(true);
    console.getFrame().setLocation(
        launcher.getX() + launcher.getWidth() + launcher.getInsets().right,
        launcher.getY());
    console.printOnConsole("BLABLABLA");


  }

  private Main2() {
    super();
    setSize(600, 600);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
}

class Console {
  final JFrame frame = new JFrame();
  JTextArea textArea = new JTextArea(24, 80);
  public Console() {

    textArea.setBackground(Color.BLACK);
    textArea.setForeground(Color.LIGHT_GRAY);
    textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
    System.setOut(new PrintStream(new OutputStream() {
      @Override
      public void write(int b) throws IOException {
        textArea.append(String.valueOf((char) b));
      }
    }));


    frame.add(textArea);
  }
  public void init() {
    frame.pack();
    frame.setVisible(true);
  }
  public JFrame getFrame() {
    return frame;
  }

  public void printOnConsole(String s){
     this.textArea.append(s);
  }
}

我添加了一个名为 printOnConsole(String s) 的函数,它将提交的字符串附加到控制台。 因此,在您的应用程序中,您可以使用 console.printToConsole("I will be displayed in JFrame Console") 调用此方法 这实际上只适用于字符串,但可以随意添加更复杂的数据结构。

最诚挚的问候

关于java - 在外部 jframe 中显示 java 控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50085582/

相关文章:

javascript - 捕获 iframe 的控制台日志

ios - Xamarin.iOS 发布版本中的 Console.WriteLine 调用会发生什么情况?

javascript - 为什么IE9打开开发者工具不能直接绑定(bind)console.log?

java - 缓存搜索结果实现

java - 将文件中的字符串值转换为整数

java - 调用模拟方法时出现 NullPointerException

java - 在另一个类的框架中启用 Jbutton - GUIBuilder

Java Swing 。前面的 Windows 从 JDialog 启动

java - 如何在 JFrame 中添加时间延迟?

java - 我的代码中的 JNDI 名称在哪里?