如何修复actionPerformed方法中的找不到符号


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyCalculator extends JFrame implements ActionListener {
static JFrame jf;
static JTextField t;
static double a = 0, b = 0, result = 0;
static int operation = 0;
MyCalculator() {
jf = new JFrame("MyCalculator");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.out.println(e.getMessage());
}
t = new JTextField(10);
t.setBounds(50, 10, 200, 50);
t.setEditable(false);

JPanel p = new JPanel();
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, badd, bsub, bmul, bdiv, beq, be, bclr;
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
b0 = new JButton("0");
badd = new JButton("+");
bsub = new JButton("-");
bmul = new JButton("*");
bdiv = new JButton("/");
beq = new JButton("=");
be = new JButton(".");
bclr = new JButton("C");
p.add(t);
p.add(bclr);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(badd);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(bsub);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(bmul);
p.add(be);
p.add(b0);
p.add(beq);
p.add(bdiv);
p.setBackground(Color.blue);
jf.add(p);
jf.setSize(210, 210);
jf.setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
badd.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
bsub.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
bmul.addActionListener(this);
be.addActionListener(this);
b0.addActionListener(this);
beq.addActionListener(this);
bdiv.addActionListener(this);
bclr.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
t.setText(t.getText().concat("1"));
}
if (e.getSource() == b2) {
t.setText(t.getText().concat("2"));
}
if (e.getSource() == b3) {
t.setText(t.getText().concat("3"));
}
if (e.getSource() == b4) {
t.setText(t.getText().concat("4"));
}
if (e.getSource() == b5) {
t.setText(t.getText().concat("5"));
}
if (e.getSource() == b6) {
t.setText(t.getText().concat("6"));
}
if (e.getSource() == b7) {
t.setText(t.getText().concat("7"));
}
if (e.getSource() == b8) {
t.setText(t.getText().concat("8"));
}
if (e.getSource() == b9) {
t.setText(t.getText().concat("9"));
}
if (e.getSource() == b0) {
t.setText(t.getText().concat("0"));
}
if (e.getSource() == be) {
t.setText(t.getText().concat("."));
}
if (e.getSource() == badd) {
a = Double.parseDouble(t.getText());
operation = 1;
t.setText("");
String s = "";
s = Double.toString(a);
}
if (e.getSource() == bsub) {
a = Double.parseDouble(t.getText());
operation = 2;
t.setText("");
String s = "";
s = Double.toString(a);
}
if (e.getSource() == bmul) {
a = Double.parseDouble(t.getText());
operation = 3;
t.setText("");
String s = "";
s = Double.toString(a);
}
if (e.getSource() == bdiv) {
a = Double.parseDouble(t.getText());
operation = 4;
t.setText("");
String s = "";
s = Double.toString(a);
}
if (e.getSource() == beq) {
b = Double.parseDouble(t.getText());
switch (operation) {
case 1:
result = a + b;
break;
case 2:
result = a - b;
break;
case 3:
result = a * b;
break;
case 4:
result = a / b;
break;
default:
result = 0;
}
t.setText("" + result);
}
if (e.getSource() == bclr) {
t.setText("");
String s = "";
t.setText("");
}
}
public static void main(String args[]) {
MyCalculator c = new MyCalculator();
jf.setVisible(true);
}
}

错误:

MyCalculator.java:104: error: cannot find symbol
if(e.getSource()==b1)
^
symbol:   variable b1
location: class MyCalculator
MyCalculator.java:108: error: cannot find symbol
if(e.getSource() == b2)
^
symbol:   variable b2
location: class MyCalculator
MyCalculator.java:112: error: cannot find symbol
if(e.getSource() == b3)
^
symbol:   variable b3
location: class MyCalculator
MyCalculator.java:116: error: cannot find symbol
if(e.getSource() == b4)
^
symbol:   variable b4
location: class MyCalculator
MyCalculator.java:120: error: cannot find symbol
if(e.getSource() == b5)
^
symbol:   variable b5
location: class MyCalculator
MyCalculator.java:124: error: cannot find symbol
if(e.getSource() == b6)
^
symbol:   variable b6
location: class MyCalculator
MyCalculator.java:128: error: cannot find symbol
if(e.getSource() == b7)
^
symbol:   variable b7
location: class MyCalculator
MyCalculator.java:132: error: cannot find symbol
if(e.getSource() == b8)
^
symbol:   variable b8
location: class MyCalculator
MyCalculator.java:136: error: cannot find symbol
if(e.getSource() == b9)
^
symbol:   variable b9
location: class MyCalculator
MyCalculator.java:140: error: cannot find symbol
if(e.getSource() == b0)
^
symbol:   variable b0
location: class MyCalculator
MyCalculator.java:144: error: cannot find symbol
if(e.getSource() == be)
^
symbol:   variable be
location: class MyCalculator
MyCalculator.java:148: error: cannot find symbol
if(e.getSource() == badd)
^
symbol:   variable badd
location: class MyCalculator
MyCalculator.java:156: error: cannot find symbol
if(e.getSource() == bsub)
^
symbol:   variable bsub
location: class MyCalculator
MyCalculator.java:164: error: cannot find symbol
if(e.getSource() == bmul)
^
symbol:   variable bmul
location: class MyCalculator
MyCalculator.java:172: error: cannot find symbol
if(e.getSource() == bdiv)
^
symbol:   variable bdiv
location: class MyCalculator
MyCalculator.java:180: error: cannot find symbol
if(e.getSource() == beq)
^
symbol:   variable beq
location: class MyCalculator
MyCalculator.java:204: error: cannot find symbol
if(e.getSource() == bclr)
^
symbol:   variable bclr
location: class MyCalculator
17 errors

您只在构造函数中定义了许多变量,但在构造函数之外使用这些变量。您必须将按钮的声明移动到构造函数之外。

class MyCalculator extends JFrame implements ActionListener
{
static JFrame jf;
static JTextField t;
static double a=0,b=0,result=0;
static int operation=0;
private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,badd,bsub,bmul,bdiv,beq,be,bclr;
MyCalculator()
{
jf = new JFrame("MyCalculator");
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
t = new JTextField(10);
t.setBounds(50,10,200,50);
t.setEditable(false);

JPanel p = new JPanel();

错误消息cannot find symbol表示您正在使用未定义的内容。在您的情况下,您使用例如变量b1,该变量不是为方法actionPerformed()定义的,而是仅在构造函数中定义的。

最新更新