gpt4 book ai didi

Java 控制台独立吗?

转载 作者:行者123 更新时间:2023-12-01 23:11:02 25 4
gpt4 key购买 nike

我用java做了一个简单的JFrame。但我也希望在打开真正的 .jar 文件的同时打开开发人员控制台。第 8 行是我想要在控制台窗口中输出的文本,该窗口应在 JFrame 接下来开始。

代码:

import javax.swing.JFrame;

public class Launcher extends JFrame {
private static final long serialVersionUID = 1L;

public static void main(String[] args) {
new Launcher().setVisible(true);
System.out.println("Started Launcher...");
}
private Launcher() {
super("DevCity13 - Launcher (Alpha)");
setSize(600, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

最佳答案

您始终可以显示自己的控制台,该控制台输出写入 System.out 的内容:

enter image description here

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;
import javax.swing.SwingUtilities;

public class Launcher extends JFrame {
private static final long serialVersionUID = 1L;

private static class Console {
private final JFrame frame;

public Console() {
frame = new JFrame();
final 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);
}

public JFrame getFrame() {
return frame;
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Console console = new Console();
console.init();
Launcher launcher = new Launcher();
launcher.setVisible(true);
console.getFrame().setLocation(
launcher.getX() + launcher.getWidth()
+ launcher.getInsets().right, launcher.getY());
System.out.println("Started Launcher...");
}
});
}

private Launcher() {
super("DevCity13 - Launcher (Alpha)");
setSize(600, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

关于Java 控制台独立吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21974596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com