2013-10-31 62 views
0

我對Java很陌生,尤其是在構建JFrame時。我已經收到上述錯誤,並且問題似乎來自生成的代碼。 請幫助我清除錯誤並瞭解導致錯誤的原因。Java錯誤錯誤的sym類型:setDefaultCloseOperation

package calculator; 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 


public class CalcForm { 

    /** 
    * Creates new form CalcForm 
    */ 
    public CalcForm() { 
     initComponents(); 
    } 



    //The Auto-Generated Code begins here 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jLabel1 = new javax.swing.JLabel(); 
     input1 = new javax.swing.JTextField(); 
     jLabel2 = new javax.swing.JLabel(); 
     input2 = new javax.swing.JTextField(); 
     btnAdd = new javax.swing.JButton(); 
     btnLess = new javax.swing.JButton(); 
     btnEquals = new javax.swing.JButton(); 
     jLabel3 = new javax.swing.JLabel(); 
     disp = new javax.swing.JTextField(); 

     //The two lines below are highlighted in RED 
     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     setTitle("Simple Calcucator"); 

     jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N 
     jLabel1.setText("First Input:"); 

     input1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       input1ActionPerformed(evt); 
      } 
     }); 

     jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N 
     jLabel2.setText("Second Input:"); 

     input2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       input2ActionPerformed(evt); 
      } 
     }); 

     btnAdd.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N 
     btnAdd.setText("+"); 
     btnAdd.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       btnAddActionPerformed(evt); 
      } 
     }); 

     btnLess.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N 
     btnLess.setText("-"); 
     btnLess.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       btnLessActionPerformed(evt); 
      } 
     }); 

     btnEquals.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N 
     btnEquals.setText("="); 
     btnEquals.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       btnEqualsActionPerformed(evt); 
      } 
     }); 

     jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N 
     jLabel3.setText("Your Answer is:"); 

     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(79, 79, 79) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
        .addComponent(btnEquals, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGroup(layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
           .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
           .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)) 
          .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)) 
         .addGap(61, 61, 61) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
          .addComponent(input1) 
          .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addGroup(layout.createSequentialGroup() 
           .addGap(116, 116, 116) 
           .addComponent(btnLess, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)) 
          .addComponent(input2) 
          .addComponent(disp)))) 
       .addContainerGap(94, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(34, 34, 34) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(input1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(42, 42, 42) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(btnLess, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(31, 31, 31) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(input2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(btnEquals, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(25, 25, 25) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(disp, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addContainerGap(29, Short.MAX_VALUE)) 
     ); 
     //This also has a Red underline 
     pack(); 
    }// </editor-fold> 



    private void input1ActionPerformed(java.awt.event.ActionEvent evt) {          
     // TODO add your handling code here: 
    }          

    private void input2ActionPerformed(java.awt.event.ActionEvent evt) {          
    }          

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {          

     btnAdd.setActionCommand("+"); 


    }          

    private void btnLessActionPerformed(java.awt.event.ActionEvent evt) {           

     btnLess.setActionCommand("-"); 

    }          

    private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {           

     btnEquals.setActionCommand("="); 
     String less = btnLess.getActionCommand(); 
     String more = btnAdd.getActionCommand(); 
     double t = 0; 
     String x = input2.getText(); 
     //parseDouble is a method used to read the given object as a double value 
     double y = Double.parseDouble(x); 

     String a = input1.getText(); 
     double b = Double.parseDouble(a); 
     if (less.equals(evt)) { 

      t = b - y; 
      disp.setText(String.valueOf(t)); 

     } else { 

      t = b + y; 
      disp.setText(String.valueOf(t)); 

     } 

    }           

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 



     /* 
     * Create and display the form 
     */ 

     java.awt.EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new CalcForm(); 



      } 
     }); 

    } 
    // Variables declaration - do not modify 
    private javax.swing.JButton btnAdd; 
    private javax.swing.JButton btnEquals; 
    private javax.swing.JButton btnLess; 
    private javax.swing.JTextField disp; 
    private javax.swing.JTextField input1; 
    private javax.swing.JTextField input2; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    // End of variables declaration 
} 

顯示的錯誤是:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: setDefaultCloseOperation 
    at calculator.CalcForm.initComponents(CalcForm.java:43) 
    at calculator.CalcForm.<init>(CalcForm.java:21) 
    at calculator.CalcForm$6.run(CalcForm.java:232) 
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 

高度讚賞幫助。

回答

1

如果在Eclipse的問題視圖中仍存在編譯錯誤,請不要嘗試運行您的應用程序。

你的類不擴展JFrame,所以它沒有任何setDefaultCloseOperation()方法。它也沒有任何的setTitle(),add()pack()等方法。您錯過了類聲明中的extends JFrame部分。