-2

私が使用した以下のコードで pct を宣言する必要があり、netbeans からエラーが発生しました。

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                   
 }
4

3 に答える 3

1

明確な答えを出すのに十分な情報はありませんが、クラスparsepctのファクトリ メソッドのようです。Percentそれが正しい場合、問題は変数宣言に正しい型を使用していないことです。正しいのは次のとおりです。

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

dこれは、型を持つ名前付きの変数を宣言しPercent、ファクトリ メソッドによって返された値をすぐに割り当てます。

于 2012-09-26T19:47:06.533 に答える
1

代わりにこれを行いたいと思います:

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

しかし、ちょっとした Java クラッシュ コースを受講することを真剣にお勧めします。

于 2012-09-26T19:47:14.907 に答える
0

プリミティブ型の変数を宣言する場合は、このチュートリアルをチェックして、クラス内で変数を宣言する方法を理解することをお勧めします..

*注: -pctは有効なプリミティブ型ではありません..そのため、コンパイラはそれを理解できません..

いくつかのクラスの参照を宣言している場合..次に、実際には名前で定義されたクラスが必要Percentです..

編集: -

あなたのコードを見た後、あなたは実際にコンパイラに知られていない他のタイプを使用しているように見えます..pctこれが私が作成しているものであることをコンパイラに伝えるために、名前で定義されたクラスが必要です..

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

の型を返すクラスparseDoubleの静的メソッドと同じように、 ..として宣言したものの型を返す静的メソッドを持つクラスが必要です(私はそれが であるべきだと思います)。DoubledoublePercentparsepct()pctPercent

于 2012-09-26T19:48:05.083 に答える