3
我正在Netbeans中編寫一個可視化應用程序,並遇到了需要一個對話框。我決定複製Netbeans自動提供的AboutBox
代碼段,只是發現這兩個代碼都會引發錯誤!簡單的JDialog導致堆棧溢出
我似乎無法追捕錯誤的來源,雖然兩者都是關閉對話框時引起的。
爲了簡單起見,因爲我相信這兩個錯誤是相關的,這裏是我的Dialog
。爲簡潔起見,我省略了initComponents()
和main(從未調用過)方法,但defaultCloseOperation是DISPOSE_ON_CLOSE
。
運行時出現堆棧溢出,我將在下面發佈。
public class AddRecipe extends javax.swing.JDialog {
/** Creates new form AddRecipe */
public AddRecipe(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
AddRecipe(java.awt.Frame parent) {
super(parent);
initComponents();
}
private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt) {
dispose();
}
// Variables declaration - do not modify
private javax.swing.JLabel Serves;
private javax.swing.JButton acceptButton;
private javax.swing.JButton cancelButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JList jList1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField nameTextField;
private javax.swing.JTextField servesTextField;
private javax.swing.JTextField timeTextField;
// End of variables declaration
}
堆棧跟蹤:
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:262)
at java.beans.Statement.invokeInternal(Statement.java:257)
at java.beans.Statement.access$000(Statement.java:56)
at java.beans.Statement$2.run(Statement.java:158)
at java.security.AccessController.doPrivileged(Native Method)
at java.beans.Statement.invoke(Statement.java:154)
at java.beans.Expression.getValue(Expression.java:115)
at java.beans.Encoder.getValue(Encoder.java:105)
at java.beans.Encoder.get(Encoder.java:225)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:110)
at java.beans.Encoder.writeObject(Encoder.java:74)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:274)
at java.beans.Encoder.writeExpression(Encoder.java:304)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:389)
at java.beans.DefaultPersistenceDelegate.doProperty(DefaultPersistenceDelegate.java:229)
at java.beans.DefaultPersistenceDelegate.initBean(DefaultPersistenceDelegate.java:264)
at java.beans.DefaultPersistenceDelegate.initialize(DefaultPersistenceDelegate.java:408)
at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:116)
at java.beans.Encoder.writeObject(Encoder.java:74)
at java.beans.XMLEncoder.writeObject(XMLEncoder.java:274)
at java.beans.Encoder.writeExpression(Encoder.java:304)
at java.beans.XMLEncoder.writeExpression(XMLEncoder.java:389)
at java.beans.DefaultPersistenceDelegate.doProperty(DefaultPersistenceDelegate.java:229)
at java.beans.DefaultPersistenceDelegate.initBean(DefaultPersistenceDelegate.java:264)
at java.beans.DefaultPersistenceDelegate.initialize(DefaultPersistenceDelegate.java:408)
(堆棧跟蹤重複幾百次)。
爲了完整起見,下面是調用Dialog的函數。再一次,這個代碼是建立在默認的Netbeans行爲模型之上的,(即使AboutBox Netbeans提供的原因是java.lang.reflect.InvocationTargetException
)。
private void newRecipeItemActionPerformed(java.awt.event.ActionEvent evt) {
if (addRecipe == null) {
JFrame mainFrame = RecipesApp.getApplication().getMainFrame();
addRecipe = new AddRecipe(mainFrame);
addRecipe.setLocationRelativeTo(mainFrame);
}
RecipesApp.getApplication().show(addRecipe);
}
爲什麼堆棧跟蹤中有XML事物?你有沒有試圖將這個簡單的JDialog作爲一個單獨的小項目運行(不是大項目的一部分)? – 2011-01-25 00:15:28