ActionListener无法识别主类中的变量

时间:2013-07-05 16:09:46

标签: java swing variables inheritance actionlistener

我正在尝试为我正在使用的Java类创建GUI工资单计算器。要求是它必须接受用户输入(一个使用JComboBox),计算每周工资并将结果添加到JTable。用户应该能够继续计算其他员工并有一个退出按钮。我在主类中创建了GUI,需要两个ActionListener,一个用于退出,一个用于计算并添加到JTable。

我的问题是,当我开始计算ActionListener时,它无法识别我在主类中设置的变量。我尝试使用主类名称DOT变量名称(PayrollCalc.empName)将它们公开,初始化它们似乎没有任何效果。代码不完整,因为我还没有开始添加到JTable,直到我可以先完成实际的计算。有没有人有任何建议?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class PayrollCalc {

    public static void main(String[] args) {

        //Declare variables used
        String empName = null, entDept = null, calcdPay = null;
        String[] empDept = {"Marketing","IT","Accounting","Development","Payroll","Facilities"};
        String columnNames[] = {"Name","Department","Pay Check"};
        String dataValues [][] = {
                {empName,entDept,calcdPay}
        };

        double wrkHours = 0;
        double empRate = 0;
        double wklyPay = 0;

        //Create JTable and scrollPane for output
        JTable table;
        JScrollPane scrollPane;
        table = new JTable (dataValues,columnNames);
        scrollPane = new JScrollPane(table);

        //Create JFrame object with title
        JFrame frame = new JFrame("Employee Payroll Calculator");

        //Create combo box for department choices
        JComboBox combo = new JComboBox(empDept);
        JTextField nameField = new JTextField(15);
        JTextField hourField = new JTextField(10);
        JTextField rateField = new JTextField(10);

        //Create JLables for input fields
        JLabel nameLbl = new JLabel ("Name:");
        JLabel hourLbl = new JLabel ("Hours:");
        JLabel rateLbl = new JLabel ("Rate:");
        JLabel deptLbl = new JLabel ("Department:");

        //Create buttons for ActionListners
        JButton exitButton= new JButton("Exit");
        exitButton.addActionListener(new exitApp());
        exitButton.setSize(5,5);

        JButton calcButton= new JButton("Calculate");
        calcButton.addActionListener(new calcApp());
        calcButton.setSize(5,5);

        //Create panels
        Panel panel1 = new Panel();
        panel1.add(nameLbl);
        panel1.add(nameField);
        panel1.add(deptLbl);
        panel1.add(combo);
        panel1.add(hourLbl);
        panel1.add(hourField);
        panel1.add(rateLbl);
        panel1.add(rateField);
        panel1.add(rateField);

        Panel panel2 = new Panel();
        panel2.add(calcButton);

        Panel panel3 = new Panel();
        panel3.add(calcButton);
        panel3.add(exitButton);

        Panel panel4 = new Panel();
        panel4.add(scrollPane, BorderLayout.CENTER);


        //creates the frame
        frame.setSize(950,200);
        frame.add(panel1,BorderLayout.NORTH);
        frame.add(panel2);
        frame.add(panel3, BorderLayout.SOUTH);
        frame.add(panel4);
        frame.setVisible(true);



    }
    //ActionListner for Exit button
    static class exitApp implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }
    //ActionListner for Calculate button
    static class calcApp implements ActionListener
    {
        public void actionPerformed(ActionEvent c)
        {
            empName = nameField.getText();
            entDept = combo.getName();
            wrkHours = Double.parseDouble(hourField.getText());
            empRate = Double.parseDouble(rateField.getText());
            wklyPay = wrkHours * empRate;
            calcdPay = new Double(wklyPay).toString();
        }

    }

}

2 个答案:

答案 0 :(得分:2)

由于您尝试在ActionListener中访问的变量 本地 到您的main(),因此它们不可见< / em>到 static 类。此外,在 static main()中初始化GUI会强制使用 statics ,因为从下面的评论中可以看出这是一个不好的做法。

因此,将初始化移至构造函数,将局部变量移至实例级别成员字段。除此之外,您还需要从静态嵌套类切换到使用内部类。

然后,您内部 ActionListener类可以使用

实例成员。

public class PayrollCalc {

    //Declare variables used
    private String empName = null, entDept = null, calcdPay = null;

    private double wrkHours = 0;
    private double empRate = 0;
    private double wklyPay = 0;

    private JComboBox combo;
    private JTextField nameField;
    private JTextField hourField;
    private JTextField rateField;

    public static void main(String[] args) {
        new PayrollCalc();
    }

    public PayrollCalc() {
        // ...
        combo = new JComboBox(empDept);
        nameField = new JTextField(15);
        hourField = new JTextField(10);
        rateField = new JTextField(10);
        // ...
    }

    // non-static ActionListener inner classes
}

答案 1 :(得分:2)

看,每个变量都有它的Scope ......并且你没有将任何东西定义为成员类变量,所以什么都不会作为全局变量。

you have defined variable inside main method () so their scope is limited to only main method这就是问题背后的原因。