java – 导致“找不到符号”的原因以及如何修复它?

前端之家收集整理的这篇文章主要介绍了java – 导致“找不到符号”的原因以及如何修复它?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直试图解决这个问题,我已经在不同的程序中运行它,所以它肯定在代码中.可能也很简单.错误

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

这是代码

  1. // Password1.java
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8. public class Password2 extends JFrame // inherits from the JFrame class
  9. {
  10. // static final variables to hold frame dimensions (in pixels)
  11. private static final int WIDTH = 400;
  12. private static final int HEIGHT = 120;
  13.  
  14. //declare labels,fields,buttons,etc.
  15. private JLabel enterLabel,validLabel,resultLabel;
  16. private JTextField pwTextField;
  17. private JButton enterB,clearB;
  18.  
  19. private EnterButtonHandler ebHandler;
  20. private ClearButtonHandler cbHandler;
  21.  
  22. public Password2() // constructor defines frame
  23. {
  24. setTitle( "Password Checker" ); // set the title of the frame
  25. setSize( WIDTH,HEIGHT ); // set the frame size
  26.  
  27. // prepare the container
  28. Container pane = getContentPane();
  29. GridLayout aGrid = new GridLayout( 3,2,5,5 ); // create a 3 row 2 column layout
  30. pane.setLayout( aGrid ); // set the layout for the frame
  31.  
  32. String password = "hello";
  33.  
  34. //instantiate JLabels
  35. enterLabel = new JLabel("Enter Password: ");
  36. validLabel = new JLabel("Validation: ");
  37. resultLabel = new JLabel("");
  38.  
  39. //instantiate text fields
  40. pwTextField = new JPasswordField( 30 );
  41.  
  42. //instantiate buttons
  43. enterB = new JButton("Enter");
  44. clearB = new JButton("Clear");
  45.  
  46. //initialize button handler
  47. ebHandler = new EnterButtonHandler();
  48. enterB.addActionListener(ebHandler);
  49.  
  50. //initialize button handler
  51. cbHandler = new ClearButtonHandler();
  52. clearB.addActionListener(cbHandler);
  53.  
  54.  
  55. pane.add(enterLabel);
  56. pane.add(pwTextField);
  57. pane.add(validLabel);
  58. pane.add(resultLabel);
  59. pane.add(enterB);
  60. pane.add(clearB);
  61.  
  62. //calls center frame method
  63. centerFrame( WIDTH,HEIGHT );
  64.  
  65. }// end constructor
  66.  
  67. //methood to center GUI on screen
  68. public void centerFrame( int frameWidth,int frameHeight)
  69. {
  70. //create toolkit object
  71. Toolkit aToolkit = Toolkit.getDefaultToolkit();
  72.  
  73. //create a dimension object with user screen information
  74. Dimension screen = aToolkit.getScreenSize();
  75.  
  76. //assign x,y position of upper left corner of frame
  77. int xUpperLeft = ( screen.width - frameWidth ) / 2;
  78. int yUpperLeft = ( screen.height - frameHeight ) / 2;
  79.  
  80. //method to position frame on user's screen
  81. setBounds( xUpperLeft,yUpperLeft,frameWidth,frameHeight );
  82. }
  83.  
  84. private class EnterButtonHandler implements ActionListener
  85. {
  86. public void actionPerformed(ActionEvent e)
  87. {
  88. String pw = pwTextField.getText();
  89.  
  90. if(pw.equals(password))
  91. {
  92. resultLabel.setText("Password Accepted");
  93. pwTextField.setText("");
  94. }
  95. else
  96. {
  97. resultLabel.setText("Password Rejected");
  98. pwTextField.setText("");
  99. }
  100. }
  101. }
  102. private class ClearButtonHandler implements ActionListener
  103. {
  104. public void actionPerformed(ActionEvent e)
  105. {
  106. resultLabel.setText("");
  107. pwTextField.setText("");
  108. }
  109.  
  110. }
  111. public static void main(String [] args)
  112. {
  113. JFrame aPassword2 = new Password2(); // create the JFrame object
  114. aPassword2.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
  115. aPassword2.setVisible(true);
  116. }
  117. } // end of class

解决方法

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

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

error: cannot find symbol […]

symbol: variable password

location: [in] class Password2.EnterButtonHandler

该范围/上下文(EnterButtonHandler)中没有命名密码.

快乐的编码.

提示:在不同的范围/上下文中有一个具有相同名称的局部变量…也许它不应该是局部变量?有关详情,请参阅The Java Tutorial: Variables

猜你在找的Java相关文章