我想製作一個java計算器。我在netbeans中編寫我的代碼。當我建立我的源代碼,它告訴我的錯誤:爲什麼找不到txtDisplay()方法?
C:\Users\Nishan\Documents\javawork\Calculator\src\Calculation\Calculation.java:215: error: cannot find symbol txtDisplay.setText(btn1Text); symbol: variable txtDisplay location: class Calculation
這裏是我的代碼:
private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {
String btn1Text;
btn1Text = txtDisplay.getText() + btn1.getText();
txtDisplay.setText(btn1Text);
}
**/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Calculation;
/**
*
* @author Nishan
*/
public class Calculation extends javax.swing.JFrame {
double firstnum;
double secondnum;
double result;
String operation;
public Calculation() {
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() {
jTextField1 = new javax.swing.JTextField();
btn1 = new javax.swing.JButton();
btn2 = new javax.swing.JButton();
btn3 = new javax.swing.JButton();
btn4 = new javax.swing.JButton();
btn5 = new javax.swing.JButton();
btn6 = new javax.swing.JButton();
btn7 = new javax.swing.JButton();
btn8 = new javax.swing.JButton();
btn9 = new javax.swing.JButton();
btn10 = new javax.swing.JButton();
btn11 = new javax.swing.JButton();
btn12 = new javax.swing.JButton();
btn13 = new javax.swing.JButton();
btn14 = new javax.swing.JButton();
btn15 = new javax.swing.JButton();
btn16 = new javax.swing.JButton();
btn18 = new javax.swing.JButton();
btn17 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
btn1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn1.setText("1");
btn1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn1ActionPerformed(evt);
}
});
btn2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn2.setText("2");
btn3.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn3.setText("3");
btn4.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn4.setText("+");
btn5.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn5.setText("4");
btn6.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn6.setText("5");
btn7.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn7.setText("6");
btn8.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn8.setText("-");
btn9.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn9.setText("7");
btn10.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn10.setText("8");
btn11.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn11.setText("9");
btn12.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn12.setText("*");
btn13.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn13.setText(".");
btn14.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn14.setText("0");
btn15.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn15.setText("C");
btn16.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn16.setText("/");
btn18.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn18.setText("=");
btn17.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
btn17.setText("+-/");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField1)
.addGroup(layout.createSequentialGroup()
.addComponent(btn1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(btn14)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btn13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(btn5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btn9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btn6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btn10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(btn7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btn11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btn8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btn12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(btn15, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btn16, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btn18, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap())
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(btn17, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(125, Short.MAX_VALUE)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn1)
.addComponent(btn2)
.addComponent(btn3)
.addComponent(btn4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn5)
.addComponent(btn6)
.addComponent(btn7)
.addComponent(btn8))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn9)
.addComponent(btn10)
.addComponent(btn11)
.addComponent(btn12))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn14)
.addComponent(btn13)
.addComponent(btn15)
.addComponent(btn16))
.addGap(16, 16, 16)
.addComponent(btn18)
.addContainerGap(24, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(240, Short.MAX_VALUE)
.addComponent(btn17)
.addGap(23, 23, 23)))
);
pack();
}// </editor-fold>
private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {
String btn1Text;
btn1Text = txtDisplay.getText() + btn1.getText();
txtDisplay.setText(btn1Text);
}
/**
* @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(Calculation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calculation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Calculation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calculation.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() {
new Calculation().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn1;
private javax.swing.JButton btn10;
private javax.swing.JButton btn11;
private javax.swing.JButton btn12;
private javax.swing.JButton btn13;
private javax.swing.JButton btn14;
private javax.swing.JButton btn15;
private javax.swing.JButton btn16;
private javax.swing.JButton btn17;
private javax.swing.JButton btn18;
private javax.swing.JButton btn2;
private javax.swing.JButton btn3;
private javax.swing.JButton btn4;
private javax.swing.JButton btn5;
private javax.swing.JButton btn6;
private javax.swing.JButton btn7;
private javax.swing.JButton btn8;
private javax.swing.JButton btn9;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
**
你在哪裏定義了'txtDisplay'? – SomeJavaGuy
請顯示[mcve]。我的猜測是'txtDisplay'在某處被聲明爲一個* local *變量。 –
方法?你用它像一個變量? –