什么导致了“找不到符号”错误,该如何修复?

6

我一直在尝试解决这个问题,已在不同的程序中运行过,因此肯定是代码问题。可能很简单。错误提示如下:

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

1
@RobW 正在尝试判断那是否是讽刺。 - Dave Newton
5个回答

11

读取错误信息,爱上错误信息。

需要一些练习,但是一段时间后就很容易看到更清晰的内容:只需将下面的粗体文本作为一个句子阅读即可 :)

错误: 无法找到符号 [...]

符号: 变量 password

位置: [在] Password2.EnterButtonHandler 类中

在该范围/上下文中没有名为password的东西(EnterButtonHandler)。

愉快的编码!


提示:在不同的作用域/上下文中有一个同名的本地变量...也许它不应该是一个本地变量?请参见Java教程:变量获取更多信息 :)

0

password 是本地的 Password2 构造函数。

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


0
你的类没有定义password。因此,在将其传递给equals方法时出现错误。

0

它找不到变量password,因为按照您的编码方式,它只存在于Password2构造函数中。您需要将password作为类成员变量或将其传递给Handler类的构造函数,以便它们可以引用它。


0
password 

是在 Password2 构造函数中声明的局部变量。它不在你的 EnterButtonHandler.actionPerformed 方法 的作用域内。将其改为实例变量以解决问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接