gpt4 book ai didi

java - 在 Java 中打开 Swing GUI 旁边的控制台

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:09:28 27 4
gpt4 key购买 nike

我正在构建一个带有 Swing GUI 的应用程序。如果您将从命令行打开此程序,将打印一些文本(信息和错误消息)。该程序只是一个普通的可执行 jar 文件,但是对于那些想要查看控制台的人,我想要一个选项来从 Swing GUI 打开一个控制台,它显示所有这些消息,由 System.out.print()。我见过几个应用程序有这样的功能,但我不知道如何做到这一点。

最佳答案

https://blogs.oracle.com/nickstephen/entry/java_redirecting_system_out_and 中解释了基础知识.该示例使用日志记录来重定向标准输出和标准错误。一个简单的方法可能是首先定义一个写入“控制台”的 OutputStream:

package outerr;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.swing.JTextArea;

public class PrintOutErrStream extends ByteArrayOutputStream {

final int maxTextAreaSize = 1000;
private JTextArea textArea;
public PrintOutErrStream(JTextArea textArea) {
this.textArea = textArea;
}

public void flush() throws IOException {
synchronized(this) {
super.flush();
String outputStr = this.toString();
super.reset();
if(textArea.getText().length() > maxTextAreaSize) {
textArea.replaceRange("", 0, 100);
}
textArea.append(outputStr);
}
}
}

那么这是一个演示程序的剩余部分:

package outerr;

import java.io.PrintStream;

public class StdOutErr extends javax.swing.JFrame {

/** Creates new form StdOutErr */
public StdOutErr() {
initComponents();
PrintOutErrStream poes = new PrintOutErrStream(this.jTextAreaOutErrLog);
System.setErr(new PrintStream(poes, true));
System.setOut(new PrintStream(poes, true));
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this
* method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

jButtonStdout = new javax.swing.JButton();
jButtonStderr = new javax.swing.JButton();
jPanelOutErrLog = new javax.swing.JPanel();
jScrollPaneOutErrLog = new javax.swing.JScrollPane();
jTextAreaOutErrLog = new javax.swing.JTextArea();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.FlowLayout());

jButtonStdout.setText("stdout");
jButtonStdout.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonStdoutActionPerformed(evt);
}
});
getContentPane().add(jButtonStdout);

jButtonStderr.setText("stderr");
jButtonStderr.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonStderrActionPerformed(evt);
}
});
getContentPane().add(jButtonStderr);

jTextAreaOutErrLog.setColumns(20);
jTextAreaOutErrLog.setRows(5);
jScrollPaneOutErrLog.setViewportView(jTextAreaOutErrLog);

javax.swing.GroupLayout jPanelOutErrLogLayout = new javax.swing.GroupLayout(jPanelOutErrLog);
jPanelOutErrLog.setLayout(jPanelOutErrLogLayout);
jPanelOutErrLogLayout.setHorizontalGroup(
jPanelOutErrLogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 835, Short.MAX_VALUE)
.addGroup(jPanelOutErrLogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPaneOutErrLog, javax.swing.GroupLayout.DEFAULT_SIZE, 835, Short.MAX_VALUE))
);
jPanelOutErrLogLayout.setVerticalGroup(
jPanelOutErrLogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 378, Short.MAX_VALUE)
.addGroup(jPanelOutErrLogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPaneOutErrLog, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE))
);

getContentPane().add(jPanelOutErrLog);

pack();
}// </editor-fold>

private void jButtonStdoutActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("message via stdout");
}

private void jButtonStderrActionPerformed(java.awt.event.ActionEvent evt) {
System.err.println("message via stderr");
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* 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(StdOutErr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(StdOutErr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(StdOutErr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(StdOutErr.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 StdOutErr().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButtonStderr;
private javax.swing.JButton jButtonStdout;
private javax.swing.JPanel jPanelOutErrLog;
private javax.swing.JScrollPane jScrollPaneOutErrLog;
private javax.swing.JTextArea jTextAreaOutErrLog;
// End of variables declaration
}

关于java - 在 Java 中打开 Swing GUI 旁边的控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15987388/

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