1

我正在尝试创建一个动作监听器,它将从动作监听器类外部提取变量信息,并使用该数据调用一个方法来进行计算。之后,我希望动作侦听器使用计算结果更新我的窗格。不幸的是,在下面的CalculateButtonHandler 部分中,我的一些变量出现“找不到符号错误”。有人可以解释为什么会这样吗?我已经标记了错误发生的位置。

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;



 public class DayCalculator extends JFrame 
  {

  //declaring variables
  private JTextField daysEntry, dEntry; 
  private CalculateButtonHandler cbHandler; 
  private ExitButtonHandler ebHandler;

 public DayCalculator ()
  {
     setTitle("Day Calculator");
     setSize (600, 350);

     //getting current day name in String format
     final String weekdayName = new SimpleDateFormat
     ("EEEE", Locale.ENGLISH).format(System.currentTimeMillis());     

     JPanel listPane = new JPanel();                        
     listPane.setPreferredSize(new Dimension (450, 250));
     listPane.setLocation(200,100);

     Container pane = getContentPane();  //creating and adding to content pane
     pane.setLayout(new GridBagLayout());


     JLabel instructions = new JLabel("Please enter number of days in" +
     " the past or future you'd like to know the day of.");

     JLabel todaysDate = new JLabel("Today is " + weekdayName +".");

     JTextField dEntry = new JTextField(10);


     JButton calculate = new JButton("Calculate"); //creating calculate button
     cbHandler = new CalculateButtonHandler();     //creating button listener
     calculate.addActionListener(cbHandler);       //instantiating listener


     JButton exit = new JButton ("Exit");          //creating Exit button
     ebHandler = new ExitButtonHandler();          //creating button listener
     exit.addActionListener(ebHandler);


     listPane.add(todaysDate);
     listPane.add(instructions);
     listPane.add(dEntry);        
     listPane.add(calculate);
     listPane.add(exit);

     pane.add(listPane);

     setDefaultCloseOperation(EXIT_ON_CLOSE);
     setVisible(true);                              //making JFrame visible

 }

  private class CalculateButtonHandler implements ActionListener
{
    public void actionPerformed (ActionEvent e)
  {

     int daysEntered = Integer.parseInt(dEntry.getText());    //getting user entry  
     int dayNumber = DayResult(daysEntered, weekdayName);     ////CANNOT FIND SYMBOL ERROR on weekdayName

     JLabel results = new JLabel("Your selected day is" + dayNumber +".");   //adding result to pane
     listpane.add(results);  //CANNOT FIND SYMBOL ERROR
     pane.repaint();         //CANNOT FIND SYMBOL ERROR                                             

   }
}


  private class ExitButtonHandler implements ActionListener        
 {
    public void actionPerformed (ActionEvent e)
  {

     System.exit(0);
  }

}


public static void main(String[] args) { 

  DayCalculator dCalc = new DayCalculator(); 

 }


 public static String DayResult(int entry, String name)    //method to calculate new day based on user entry
    {

     int number;

    if (name == "Monday")

      number = 1;

    else if (name == "Tuesday")

      number = 2;

    else if (name == "Wednesday")

      number = 3;

    else if (name == "Thursday")

      number = 4;

    else if (name == "Friday")

      number = 5;

    else if (name == "Saturday")

      number = 6;

    else if (name == "Sunday")

      number = 7;

     System.out.println(number); //testing  



    int dayResult = (entry+number)-7;

    if (dayResult <0){
     dayResult = -dayResult; 
     }
    String dayNameResult;

    switch (dayResult){

    case 1: dayNameResult = "Monday";
            break;
    case 2: dayNameResult = "Tuedsay";
            break;
    case 3: dayNameResult = "Wednesday";
            break;
    case 4: dayNameResult = "Thursday";
            break;
    case 5: dayNameResult = "Friday";
            break;
    case 6: dayNameResult = "Saturday"; 
            break;
    case 7: dayNameResult = "Sunday"; 
           break;
    }

     return (dayNameResult);
  }
}

错误是:

   DayCalculator.java:69: error: cannot find symbol
     int dayNumber = DayResult(daysEntered, weekdayName);     
     symbol:   variable weekdayName
     location: class DayCalculator.CalculateButtonHandler

   DayCalculator.java:72: error: cannot find symbol
     listpane.add(results);
     ^
    symbol:   variable listpane
    location: class DayCalculator.CalculateButtonHandler

  DayCalculator.java:73: error: cannot find symbol
     pane.repaint();                                                       
     ^
    symbol:   variable pane
    location: class DayCalculator.CalculateButtonHandler
    3 errors
4

0 回答 0