什么原因导致“找不到符号”以及如何解决它?
我一直试图弄清楚这一点,我已经在不同的程序中运行过它,因此它肯定在代码中。可能也很容易。错误说
Password2.java:90:错误:找不到符号if(pw.equals(password))^符号:变量密码位置:类Password2.EnterButtonHandler
1错误
这是代码:
// Password1.javaimport 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
回答:
阅读错误消息,喜欢错误消息。
这需要一些练习,但是过一会儿就很容易看清楚:只需将下面的粗体作为句子阅读即可:)
错误: […]
符号:可变
位置: Password2类中。
password
该作用域/上下文 (EnterButtonHandler
)中未命名。
快乐的编码。
提示:在 不同的 作用域/上下文中有一个具有相同名称的 局部 变量……也许它不应该是 局部
变量?有关更多信息,请参见Java教程:变量。
以上是 什么原因导致“找不到符号”以及如何解决它? 的全部内容, 来源链接: utcz.com/qa/435170.html