使用actionlistener中的变量到类

时间:2014-10-06 08:39:24

标签: java actionlistener

帮助我迷失了我仍然无法使用之前发起的变量,我不知道我是否正确地做到了这一点我仍然是java的新手。我一直试图建立一个薪资系统,而且我一直在努力从动作监听器中读取一个变量。提前致谢!

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.print.attribute.SetOfIntegerSyntax;
import javax.swing. * ;
import java.awt. * ;
import java.awt.event. * ;



 @SuppressWarnings("serial")
 public class kapoy extends JFrame {

    public static JTextField text1;
    public static JTextField text2;
    public JLabel label1;
    public JLabel label2;
    public JPanel panel1;
    public JPanel panel2;
    public JPanel panel3;
    public JPanel panel4;
    public kapoy() {
        text1 = new JTextField();
        text1.setPreferredSize(new Dimension(50, 20));
        text2 = new JTextField();
        text2.setPreferredSize(new Dimension(50, 20));
        label1 = new JLabel("Inpute Employee ID: ");
        label2 = new JLabel("Input worked Days: ");
        panel1 = new JPanel();
        panel1.setLocation(0, 0);
        panel1.setSize(300, 40);
        panel1.setBackground(Color.blue);
        panel1.add(label1);
        panel1.add(text1);
        add(panel1);
        panel2 = new JPanel();
        panel2.setLocation(0, 40);
        panel2.setSize(300, 40);
        panel2.setBackground(Color.red);
        panel2.add(label2);
        panel2.add(text2);
        add(panel2);
        panel3 = new JPanel();
        panel3.setLocation(0, 80);
        panel3.setSize(400, 200);
        panel3.setBackground(Color.green);
        add(panel3);
        panel4 = new JPanel();
        panel4.setLocation(300, 0);
        panel4.setSize(100, 80);
        panel4.setBackground(Color.yellow);
        add(panel4);
        setSize(410, 300);
        setLayout(null);
        setTitle("Pay Roll by Migz");
    }

    public static void main(String[]args) {

        kapoy cn = new kapoy();
        cn.setVisible(true);

        text1.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                try {

                    int idnum = Integer.parseInt(text1.getText());
                    bweset ka = new bweset(idnum);

                } catch (NumberFormatException nfe) {
                    text1.setText("");
                }
            }
        });

        try {
            File f = new File("D:/Users/DAVID Family/Desktop/Employees.txt");
            Scanner sc = new Scanner(f);

            List < Employee > people = new ArrayList < Employee > ();
            while (sc.hasNextLine()) {

                String line = sc.nextLine();
                String[]details = line.split(" ");
                int Id = Integer.parseInt(details[0]);
                String name = details[1];
                int rate = Integer.parseInt(details[2]);
                Employee p = new Employee(Id, name, rate);

                if (ka == Id) {
                    people.add(p);
                }
            }

            for (Employee p : people) {
                System.out.println(p.toString());
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

}

class bweset {
    private int ka;

    public bweset(int ka) {
        this.ka = ka;
    }
    public int getka() {
        return ka;
    }
    public void setka(int ka) {
        this.ka = ka;
    }
    public int ka() {
        return this.ka;
    }
    public int toint() {
        return this.ka;
    }
}
class Employee {

    private int Id;
    private String name;
    private int rate;

    public Employee(int Id, String name, int rate) {
        this.Id = Id;
        this.setName(name);
        this.rate = rate;
    }

    public int getId() {
        return Id;
    }

    public void setId(int Id) {
        this.Id = Id;
    }

    /**  
     * @param name the name to set  
     */
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public int getrate() {
        return rate;
    }

    public void setrate(int rate) {
        this.rate = rate;
    }

    public String toString() {
        return this.Id + " " + this.name + " " + this.rate;
    }
}

0 个答案:

没有答案