Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何修复actionPerformed方法中的找不到符号?_Java_Error Handling_Actionlistener - Fatal编程技术网

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

Java 如何修复actionPerformed方法中的找不到符号?,java,error-handling,actionlistener,Java,Error Handling,Actionlistener,错误: 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

错误:

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

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

请您的问题正确格式化您的代码。这里的问题是,您引用的
actionPerformed
方法中的所有
JButton
s,但是这些按钮仅在
MyCalculator
的构造函数的范围内。有关的详细信息,请参阅。若要解决此问题,请将其设置为类级别变量。请在问题中添加一些文本,以便下次描述您的问题,而不仅仅是代码和堆栈跟踪。
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