我知道输出可以保存到文件中,但我想知道 Netbeans 的输出结果是否可以显示在表单、文本框或其他内容中。
可能吗?
谢谢,
恰帕
这是一个非常简化的示例,但是是的。可以将结果显示在表单中。
您需要做的是创建一个新表单(我使用了 JFrame 表单)。从那里,我在表单中添加了一个 JTextPane。
这应该是您在表单上需要的全部内容,只是为了显示结果。
之后,您只需将 textPane 的文本设置为您的结果并运行程序。
这就是我的代码的样子。
公共类 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 中创建表单的教程的链接: 表单创建教程