2012-09-26 2 views
-2

나는 내가 사용하고 넷빈즈에서 오류를 입수했습니다 그 아래의 코드에서 PCT를 선언해야합니다변수를 선언하는 방법은 무엇입니까?

pct d = Percent.parsepct(discountpercentTextField.getText()); 

은 무엇 PCT 선언에 대한 정확한 코드 것입니까? 또한, 내가 이것을 선언해야합니까? 처음에는?

여기에 전체 코드 파일입니다

 /* 
     * To change this template, choose Tools | Templates 
     * and open the template in the editor. 
     */ 

     package murach.forms; 
import murach.business.InvoiceCalculations; 
import java.text.NumberFormat; 


/** 
* 
* @author james 
*/ 
public class InvoiceForm extends javax.swing.JFrame { 

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

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

    messageLabe1 = new javax.swing.JLabel(); 
    messageLabe2 = new javax.swing.JLabel(); 
    messageLabe3 = new javax.swing.JLabel(); 
    messageLabe4 = new javax.swing.JLabel(); 
    messageLabe5 = new javax.swing.JLabel(); 
    customertypeTextField = new javax.swing.JTextField(); 
    subtotalTextField = new javax.swing.JTextField(); 
    discountpercentTextField = new javax.swing.JTextField(); 
    discountamountTextField = new javax.swing.JTextField(); 
    totalTextField = new javax.swing.JTextField(); 
    calculateButton = new javax.swing.JButton(); 
    exitButton = new javax.swing.JButton(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    setTitle("Invoice Total Calculator"); 

    messageLabe1.setText("Customer Type:"); 

    messageLabe2.setText("Subtotal:"); 

    messageLabe3.setText("Discount Percent:"); 

    messageLabe4.setText("Discount Amount:"); 

    messageLabe5.setText("Total:"); 

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

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

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

    calculateButton.setMnemonic('c'); 
    calculateButton.setText("Calculate"); 
    calculateButton.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      calculateButtonActionPerformed(evt); 
     } 
    }); 

    exitButton.setMnemonic('x'); 
    exitButton.setText("Exit"); 
    exitButton.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      exitButtonActionPerformed(evt); 
     } 
    }); 

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
     .add(layout.createSequentialGroup() 
      .add(38, 38, 38) 
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
       .add(messageLabe1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
       .add(messageLabe2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
       .add(messageLabe3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 126, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
       .add(messageLabe4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 137, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
       .add(messageLabe5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 
      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
       .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) 
        .add(totalTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE) 
        .add(discountamountTextField)) 
       .add(org.jdesktop.layout.GroupLayout.TRAILING, discountpercentTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 144, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
       .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
        .add(customertypeTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
        .add(subtotalTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 144, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))) 
      .addContainerGap(74, Short.MAX_VALUE)) 
     .add(layout.createSequentialGroup() 
      .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
      .add(calculateButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 124, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 
      .add(exitButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 77, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
      .add(43, 43, 43)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
     .add(layout.createSequentialGroup() 
      .add(31, 31, 31) 
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 
       .add(messageLabe1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
       .add(customertypeTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 
      .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 
       .add(messageLabe2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
       .add(subtotalTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 
      .add(6, 6, 6) 
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 
       .add(messageLabe3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
       .add(discountpercentTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 
      .add(6, 6, 6) 
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 
       .add(messageLabe4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
       .add(discountamountTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 
      .add(6, 6, 6) 
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 
       .add(messageLabe5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
       .add(totalTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 
      .add(18, 18, 18) 
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 
       .add(exitButton) 
       .add(calculateButton)) 
      .addContainerGap(58, Short.MAX_VALUE)) 
    ); 

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

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

}              

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

}             

private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
    System.exit(0); 


}           
private boolean isValidData() 
{ 
    SwingValidator sv= new SwingValidator(); 
    return 
     sv.isPresent(customertypeTextField, "Customer Type")&& 
     sv.isPresent(subtotalTextField, "Subtotal")&& 
     sv.isDouble(subtotalTextField, "Subtotal");  
}   


private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {             
     // TODO add your handling code here: 
    double c = Double.parseDouble(customertypeTextField.getText()); 
    double s = Double.parseDouble(subtotalTextField.getText()); 
    pct d = Percent.parsepct(discountpercentTextField.getText()); 
    double a = Double.parseDouble(discountamountTextField.getText()); 
    double total = InvoiceCalculations.calculateTotal(c, s, d); 
    NumberFormat currency = NumberFormat.getCurrencyInstance(); 
    totalTextField.setText(currency.format(total)); 
}            

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

}               

/** 
* @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(InvoiceForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(InvoiceForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(InvoiceForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(InvoiceForm.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() { 
      InvoiceForm frame =new InvoiceForm(); 
      frame.setVisible (true); 
      frame.setLocationRelativeTo(null); 
     } 
    }); 
} 
// Variables declaration - do not modify      
private javax.swing.JButton calculateButton; 
private javax.swing.JTextField customertypeTextField; 
private javax.swing.JTextField discountamountTextField; 
private javax.swing.JTextField discountpercentTextField; 
private javax.swing.JButton exitButton; 
private javax.swing.JLabel messageLabe1; 
private javax.swing.JLabel messageLabe2; 
private javax.swing.JLabel messageLabe3; 
private javax.swing.JLabel messageLabe4; 
private javax.swing.JLabel messageLabe5; 
private javax.swing.JTextField subtotalTextField; 
private javax.swing.JTextField totalTextField; 
// End of variables declaration     
} 
+3

* 어떤 오류가 발생 했습니까? –

+0

당신의'pct '는 무엇입니까? 여기에서는'pct'를 선언하지 않습니다. 대신'pct' 타입의 변수입니다. 그래서,'pct' 타입을 정의 했습니까? 또는 변수로 사용하는 경우에는 사이에 공백이 없어야합니다. –

+0

나는 그것을 선언해야합니다. netbeans can not fina symbol –

답변

1

명확한 대답을 줄 수있는 충분한 정보가 아니라, parsepctPercent 클래스의 팩토리 메소드 인 것 같습니다. 그렇다면 변수 선언에 올바른 유형을 사용하지 않는 것이 문제입니다. 올바른 것 :

Percent d = Percent.parsepct(discountpercentTextField.getText()); 

는이 유형 Percent을 가지고 d라는 변수를 선언하고 즉시 그것을 팩토리 메소드에 의해 반환 된 값을 할당합니다.

Percent pct = Percent.parsepct(discountpercentTextField.getText()); 

를하지만 심각하게당신이 약간의 자바 충돌 코스를 수강 추천 :

+0

pct가 합법적 인 것으로 보입니다. http://stackoverflow.com/questions/12605994/gui-calculate-button-coding-error-in-java –

1

난 당신이 대신이 작업을 수행한다고 가정한다.

0

일부 원시 형의 변수를 선언하려면 다음 클래스 내에 변수를 선언하는 방법을 이해하려면이 tutorial을 확인 하는 것 같아서 ..

* 참고 : - pct가 아닌 유효한 원시 형 .. 그래서 컴파일러는 그것을 이해할 수 없다 ..

을 당신이 어떤 클래스에 대한 참조를 선언하는 경우 .. 그럼 당신은 실제로 이름 Percent으로 정의 된 몇 가지 클래스가 필요 ..

편집가 : - 당신이 실제로 ... 당신은 이름 pct으로 정의 된 클래스가 있어야 컴파일러에 알려지지 않은 다른 유형을 사용하는 것처럼

코드를 본 후, 보이는, 이것이 그 컴파일러에게 제가 .. 그냥 parseDouble 같은

double s = Double.parseDouble(subtotalTextField.getText()); 
    pct d = Percent.parsepct(discountpercentTextField.getText()); 

를 작성하고있어 double의 유형을 반환 Double 클래스의 정적 메서드, 당신은 어떤 유형을 반환 정적 메서드 parsepct(), 함께 Percent 클래스가 필요하다 당신은 pct으로 선언했습니다. (나는 d는 Percent 임).

+0

내 투표가 아니지만 더 제공하지 않는 한 댓글이어야합니다. 코멘트 이상의 의미를 갖는 유용한 정보. –

+0

내 downvote. "외부 사이트에 대한 링크 이상의 간신히"는 대답에 적합하지 않습니다. 그것은 faq에 바로 있습니다. –

+0

'pct '라는 이름의 클래스가있을 가능성이 있습니다 - 명명 규칙에 위배되지만 구문 적으로는 유효합니다. –

관련 문제