是什么原因导致“找不到符号”?以及如何解决它?

发布于 2024-12-14 12:47:36 字数 3851 浏览 1 评论 0原文

我一直在试图解决这个问题,我在不同的程序中运行过它,所以它肯定在代码中。也许也很容易。错误说

Password2.java:90: 错误: 找不到符号 if(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

I've been trying to figure this out, I've run it in different programs so it's definitely in the code. Probably something easy too. The error says

Password2.java:90: error: cannot find symbol
if(pw.equals(password))
^
symbol: variable password
location: class Password2.EnterButtonHandler
1 error

Here is the code:

// 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

碍人泪离人颜 2024-12-21 12:47:36

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

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

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

符号:变量密码

位置:[在]类Password2.EnterButtonHandler

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

快乐编码。


提示:在不同范围/上下文中有一个同名的本地变量...也许它不应该是本地变量?有关更多信息,请参阅 Java 教程:变量 :)

Read the error message, love the error message.

It takes some practice, but after awhile it's easy to see it more clearly: just read across the bold text below as a sentence :)

error: cannot find symbol [...]

symbol: variable password

location: [in] class Password2.EnterButtonHandler

There is nothing named password in that scope/context (EnterButtonHandler).

Happy coding.


Hint: there is a local variable with the same name in a different scope/context... perhaps it shouldn't be a local variable? See The Java Tutorial: Variables for more :)

咿呀咿呀哟 2024-12-21 12:47:36

passwordPassword2 构造函数的本地内容。

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

password is local to the Password2 constructor.

It should either be passed around, or an instance variable.

夜光 2024-12-21 12:47:36

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

Your class doesn't have a definition for password. Hence, the error when passing it to the equals method.

格子衫的從容 2024-12-21 12:47:36

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

It can't find the variable password, which, as you've coded it, only exists in the Password2 constructor. You'll need to either make password a class member variable or pass it to the constructor of your Handler classes, so they can have a reference to it.

鹿! 2024-12-21 12:47:36
password 

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

password 

is a local variable declared in the constructor of Password2. It is not in scope in your EnterButtonHandler.actionPerformed method. Make it an instance variable to resolve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文