java - 如何以表单形式显示 Netbeans 的输出?

标签 java netbeans output

我知道输出可以保存到文件中,但我想知道 Netbeans 的输出结果是否可以显示在表单、文本框或其他内容中。

可能吗?

谢谢

恰帕

最佳答案

这是一个非常简单的例子,但是是的。可以将结果显示在表单中。

您需要做的是创建一个新表单(我使用了 JFrame 表单)。 从那里,我将 JTextPane 添加到表单中。

这应该是您表单上所需的全部内容,只是为了显示结果。

之后,您只需将 textPane 的文本设置为您的结果并运行程序即可。

这就是我的代码的样子。

公共(public)类 ResultsForm 扩展 javax.swing.JFrame {

/**
 * Creates new form ResultsForm
 */
public ResultsForm() {
    initComponents();
    this.TextArea.setText("No input");
    this.TextArea.setEditable(false);
}

//This is the constructor that takes in your results and places is
//in the form
public ResultsForm(String results){
    initComponents();
    this.TextArea.setText(results);
    this.TextArea.setEditable(false);
}

/**
 * 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() {

    jScrollPane2 = new javax.swing.JScrollPane();
    TextArea = new javax.swing.JTextPane();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jScrollPane2.setViewportView(TextArea);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(64, 64, 64)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(64, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(60, 60, 60)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(98, Short.MAX_VALUE))
    );

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

/**
 * @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(ResultsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(ResultsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(ResultsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(ResultsForm.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() {
            //This is where you should place your running code
    String dataToOutput = "This should be on the form";
            //This is where you pass in the results to the form
    new ResultsForm(dataToOutput).setVisible(true);
    }
});
}
    // Variables declaration - do not modify                     
    private javax.swing.JTextPane TextArea;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration                   
}

此外,这里还有一个关于在 Netbeans 中创建表单的非常基本的教程的链接: Form Creation Tutorial

关于java - 如何以表单形式显示 Netbeans 的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17575233/

相关文章:

java - 在运行时交换 Jackson 自定义序列化器/反序列化器

c++ - GLFW无法编译示例NetBeans Mingw W64

PHP Delete 返回错误

清理 argv 程序

c++ - Ofstream 拒绝带参数的输入

java - 调度程序未在 Spring Boot 中运行

java - 从java调用ant,ant终止后返回java

java - 如何找到一部分网格的坐标?

java - 如何根据窗口调整 jlabel 的大小

java - 我的 JApplet 中的线程 "AWT-EventQueue-1"java.lang.NullPointerException 中出现异常