如何访问JButton ActionListener中的变量?

时间:2015-04-25 12:25:40

标签: java swing jbutton actionlistener

希望这不是一件容易的事,我是java的新手。我无法弄清楚如何访问" iDaysAlive" ActionListener之外的变量。

    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; //this variable
        }
    });

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

    JLabel lblDaysAlive = new JLabel("Days Alive: " + iDaysAlive);

    //iDaysAlive cannot be resolved to a variable

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

4 个答案:

答案 0 :(得分:1)

你不能,变量iDaysAlive仅在actionPerformed方法的范围内可见。

即使您将其添加为类的字段,也无法访问它,因为ActionListener的结构非常复杂。

实现一个实现ActionListener的类,如

class MyCoolActionListener implements ActionListener
{
double iDaysAlive;
@Override
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; //this variable
    }
public double getIDaysAlive()
{
return iDaysAlive;
}
}

并将iDaysAlive添加为字段。编写getter和setter方法并像

一样使用它
MyCoolActionListener al = new MyCoolActionListener()
jButton_Calculate.addActionListener(al);
al.getIDaysAlive();

编辑:或者更容易:定义您需要访问actionListener

之外的变量

答案 1 :(得分:1)

如果要访问函数外部的变量,则必须在函数外部声明变量。只需在函数

之前写double iDaysAlive;
double iDaysAlive; // Declare outside
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());


        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; //this variable
    }
});

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

JLabel lblDaysAlive = new JLabel("Days Alive: " + iDaysAlive);

//you have now acces to the variable iDaysAlive

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

答案 2 :(得分:1)

要访问匿名类之外的变量,必须在外部声明它,并且必须是final

在你的情况下这不起作用,因为如果你进行双冠决赛,你就无法改变它。因此,您可以使用具体类而不是匿名类将结果存储在属性中以便稍后访问它。或者,您可以为结果使用辅助类,该结果可以声明为final,并保存要从匿名动作侦听器更改的属性。或者你可以简单地将它作为封闭类的一个属性。

答案 3 :(得分:1)

double iDaysAlive;

这应该在函数之外声明,更优选地在类的顶部声明。实际上,您应该阅读有关全局变量和局部变量的内容,因为这会对您有所帮助。平均时间,地点

double iDaysAlive = 0.0;

在课程开始时它应该没问题。