Java 是什么原因引起的;Can';“找不到符号”;如何修复它?

Java 是什么原因引起的;Can';“找不到符号”;如何修复它?,java,compiler-errors,Java,Compiler Errors,我一直在试图弄明白这一点,我在不同的程序中运行过它,所以它肯定在代码中。可能也很简单。错误是 Password2.java:90:错误:找不到符号 if(pw.equals(密码)) ^ 符号:可变密码 位置:类密码2.EnterButtonHandler 1错误 代码如下: // Password1.java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.

我一直在试图弄明白这一点,我在不同的程序中运行过它,所以它肯定在代码中。可能也很简单。错误是

Password2.java:90:错误:找不到符号 if(pw.equals(密码)) ^ 符号:可变密码 位置:类密码2.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

password
对于
Password2
构造函数是本地的


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

您的类没有
密码的定义。因此,将其传递给
方法时出现的错误等于
方法。

它找不到变量
password
,在您对其进行编码时,该变量仅存在于
Password2
构造函数中。您需要将
password
设置为类成员变量,或者将其传递给
处理程序的构造函数,以便它们可以引用它。

阅读错误消息,热爱错误消息

这需要一些练习,但过一段时间后很容易看得更清楚:只需将下面的粗体文本作为一个句子阅读:)

错误:找不到符号[…]

符号:变量密码

位置:[in]类密码2.输入按钮句柄

在该范围/上下文中没有名为
password
的内容(
EnterButtonHandler

快乐编码


提示:在不同的作用域/上下文中存在同名的局部变量。。。也许它不应该是一个局部变量?有关详细信息,请参阅:)


是在Password2的构造函数中声明的局部变量。它不在
EnterButtonHandler.actionPerformed方法的范围内。将其作为要解析的实例变量。

@RobW正在尝试确定这是否是讽刺-->
password