2013-07-10 47 views

回答

2

這是一個非常簡單的例子,但肯定的。您可以將結果顯示在表單中。

你需要做的是創建一個新的窗體(我使用了一個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創建窗體: Form Creation Tutorial

相關問題