【问题标题】:What causes "Can't find Symbol" and how to fix it?是什么导致“找不到符号”以及如何解决?
【发布时间】:2011-11-10 20:58:02
【问题描述】:

我一直试图弄清楚这一点,我已经在不同的程序中运行它,所以它肯定在代码中。可能也很容易。错误说

Password2.java:90:错误:找不到符号 如果(pw.equals(密码)) ^ 符号:可变密码 位置:类 Password2.EnterButtonHandler 1 个错误

代码如下:

// Password1.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Password2 extends JFrame // inherits from the JFrame class 
{
    // static final variables to hold frame dimensions (in pixels) 
    private static final int WIDTH = 400;
    private static final int HEIGHT = 120;

    //declare labels, fields, buttons, etc.
    private JLabel enterLabel, validLabel, resultLabel;
    private JTextField pwTextField;
    private JButton enterB, clearB;

    private EnterButtonHandler ebHandler;
    private ClearButtonHandler cbHandler;

    public Password2() // constructor defines frame 
    { 
            setTitle( "Password Checker" ); // set the title of the frame
        setSize( WIDTH, HEIGHT ); // set the frame size

        // prepare the container 
        Container pane = getContentPane();
        GridLayout aGrid = new GridLayout( 3, 2, 5, 5 ); // create a 3 row 2 column layout
        pane.setLayout( aGrid ); // set the layout for the frame

        String password = "hello";

        //instantiate JLabels
        enterLabel = new JLabel("Enter Password: ");
        validLabel = new JLabel("Validation: ");
        resultLabel = new JLabel("");

        //instantiate text fields
        pwTextField = new JPasswordField( 30 );

        //instantiate buttons
        enterB = new JButton("Enter");
        clearB = new JButton("Clear");

        //initialize button handler
        ebHandler = new EnterButtonHandler();
        enterB.addActionListener(ebHandler);

        //initialize button handler
        cbHandler = new ClearButtonHandler();
        clearB.addActionListener(cbHandler);


        pane.add(enterLabel);
        pane.add(pwTextField);
        pane.add(validLabel);
        pane.add(resultLabel);
        pane.add(enterB);
        pane.add(clearB);

        //calls center frame method
        centerFrame( WIDTH, HEIGHT );

    }// end constructor

    //methood to center GUI on screen
    public void centerFrame( int frameWidth, int frameHeight)
    {
        //create toolkit object
        Toolkit aToolkit = Toolkit.getDefaultToolkit();

        //create a dimension object with user screen information
        Dimension screen = aToolkit.getScreenSize();

        //assign x, y position of upper left corner of frame
        int xUpperLeft = ( screen.width - frameWidth ) / 2;
        int yUpperLeft = ( screen.height - frameHeight ) / 2;

        //method to position frame on user's screen
        setBounds( xUpperLeft, yUpperLeft, frameWidth, frameHeight );
    }

    private class EnterButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String pw = pwTextField.getText();

            if(pw.equals(password))
            {
                resultLabel.setText("Password Accepted");
                pwTextField.setText("");
            }
            else
            {
                resultLabel.setText("Password Rejected");
                pwTextField.setText("");
            }
        }
    }
    private class ClearButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            resultLabel.setText("");
            pwTextField.setText("");
        }

    }
    public static void main(String [] args) 
    {
        JFrame aPassword2 = new Password2(); // create the JFrame object
        aPassword2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        aPassword2.setVisible(true);
    }
    } // end of class

【问题讨论】:

  • @RobW 试图确定这是否是讽刺 -->

标签: java compiler-errors


【解决方案1】:

阅读错误信息,喜欢错误信息。

这需要一些练习,但过一段时间就会更清楚地看到它:只需阅读下面的粗体文本作为一个句子:)

错误:找不到符号 [...]

符号:变量密码

位置:[in] 类 Password2.EnterButtonHandler

在该范围/上下文中没有任何名称为 password (EnterButtonHandler)。

编码愉快。


提示:在 不同 范围/上下文中有一个同名的 local 变量...也许它不应该是 local em>变量?有关更多信息,请参阅The Java Tutorial: Variables :)

【讨论】:

    【解决方案2】:

    passwordPassword2 构造函数的本地。

    它应该被传递,或者是一个实例变量。

    【讨论】:

      【解决方案3】:

      您的班级没有password 的定义。因此,将其传递给 equals 方法时会出错。

      【讨论】:

        【解决方案4】:

        它找不到变量password,正如您编写的那样,它只存在于Password2 构造函数中。您需要将 password 设为类成员变量或将其传递给 Handler 类的构造函数,以便它们可以引用它。

        【讨论】:

          【解决方案5】:
          password 
          

          是在 Password2 的构造函数中声明的局部变量。它不在您的EnterButtonHandler.actionPerformed method 范围内。让它成为一个实例变量来解析。

          【讨论】:

            猜你喜欢
            • 2011-05-20
            • 1970-01-01
            • 2023-01-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-07-03
            • 1970-01-01
            • 2021-03-07
            相关资源
            最近更新 更多