如何在ActionListener中访问此变量?

时间:2015-04-26 02:58:49

标签: java actionlistener

public class DaysAliveGUI {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    DaysAliveGUI window = new DaysAliveGUI();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public DaysAliveGUI() {
        initialize();
    }

    private void initialize() {

        JButton jButton_Calculate = new JButton("Calculate");
        jButton_Calculate.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent arg0) {

                int iDayBirth = Integer.parseInt(jTextField_DoBDay.getText());
                int iMonthBirth = Integer.parseInt(jTextField_DoBMonth.getText());
                int iYearBirth = Integer.parseInt(jTextField_DoBYear.getText());

                int iDayCurrent = Integer.parseInt(jTextField_CdDay.getText());
                int iMonthCurrent = Integer.parseInt(jTextField_CdMonth.getText());
                int iYearCurrent = Integer.parseInt(jTextField_CdYear.getText());

                double iDaysAlive;

                Calendar caBirthDate = new GregorianCalendar(iYearBirth, iMonthBirth - 1, iDayBirth);
                Calendar caCurrentDate = new GregorianCalendar(iYearCurrent, iMonthCurrent - 1, iDayCurrent);

                iDaysAlive = caCurrentDate.getTimeInMillis() - caBirthDate.getTimeInMillis();
    iDaysAlive = iDaysAlive / (24 * 60 * 60 * 1000) + 1;
            }
        });

        jButton_Calculate.setFont(new Font("Calibri", Font.BOLD, 15));
        jButton_Calculate.setBounds(180, 205, 103, 23);
        frame.getContentPane().add(jButton_Calculate);

        /* Here is the error:
           iDaysAlive cannot be resolved to a variable*/
        JLabel lblDaysAlive = new JLabel("Days Alive: " + iDaysAlive);

        lblDaysAlive.setFont(new Font("Calibri", Font.BOLD, 18));
        lblDaysAlive.setBounds(135, 257, 147, 21);
        frame.getContentPane().add(lblDaysAlive);

    }
}

3 个答案:

答案 0 :(得分:1)

您正在使用行

在动作侦听器中声明iDaysAlive
        double iDaysAlive;

如果在动作监听器中声明它,则无法在外部使用它。

您需要将iDaysAlive定义在inialize函数可以访问的位置。我建议把它变成你班上的一个领域。

答案 1 :(得分:1)

你可以将你的变量作为一个全局变量,使其在类中可以访问..

您可以在方法之外初始化iDaysAlive

public class DaysAliveGUI {
   double iDaysAlive = 0;
public static void main(String[] args) {
 ....

您也可以将其设为static variable并创建gettersetter

像这样:

static double iDaysAlive;

 public static void setIDaysAlive(double _iDaysAlive ){
   iDaysAlive = _iDaysAlive;
 }
 public static double getIDaysAlive(){
    return iDaysAlive;
 }

如果你想分配一个值,你可以这样做:

setIDaysAlive(123);

如果我们要实施您的公式,它将如下所示:

setIDaysAlive( caCurrentDate.getTimeInMillis() - caBirthDate.getTimeInMillis() );

setIDaysAlive( getIDaysAlive() / (24 * 60 * 60 * 1000) + 1 );

如果你想获得价值,你可以这样称呼:

getIDaysAlive();

<强>更新 这里是添加静态变量以及getter和setter方法的地方:

static double iDaysAlive;

  public static void setIDaysAlive(double _iDaysAlive ){
   iDaysAlive = _iDaysAlive;
 }
 public static double getIDaysAlive(){
    return iDaysAlive;
 }
public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    DaysAliveGUI window = new DaysAliveGUI();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

答案 2 :(得分:1)

由于您已在动作侦听器下方声明lblDaysAlive,因此无法在侦听器中访问它。正如其他人所建议的那样,通过声明类范围来使其成为实例变量。

如果您仍想在方法中声明它,请尝试将声明与jButton_Calculate一起移动:

JButton jButton_Calculate = new JButton("Calculate");
JLabel lblDaysAlive = new JLabel("Days Alive: --");//move here
...
jButton_Calculate.addActionListener(new ActionListener() {
    public void actionPerformed (ActionEvent arg0) {
      ...
      lblDaysAlive.setText("Days Alive: " + iDaysAlive);
    }
});
...