将值从JTextField传递给ActionListener

时间:2014-07-10 07:52:37

标签: java swing events jdbc

我正在编写一个用于人力资源管理的小型GUI程序,类Insert是在按钮"提交"单击,我有问题将从JTextField读取的值传递给HRActionListener,这是从ActionListener实现的,它只允许一个参数,我试图包含所有按钮事件一节课。

以下是插入, AddGui and HRActionListener`

的代码
public class Insert {
private int id;
private String name;
private String sex;        
private String department;
private int salary;

public Insert(int id, String name, String sex, String department, int salary){
    this.id = id; 

    this.name = name;
    this.sex = sex;
    this.department = department;
    this.salary = salary;

}



public void insert(){
    Connection conn = DBconn.getConn();
    String sql = "insert into person values(?,?,?,?,?)";
    PreparedStatement preparedStatement = null;             
    try {
        preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setInt(1,id);
        preparedStatement.setString(2,name);
        preparedStatement.setString(3,sex);
        preparedStatement.setString(4,department);
        preparedStatement.setInt(5,salary);
        //update
        int rowsAffected = preparedStatement.executeUpdate();
    } catch (SQLException ex) {
        System.out.println("增加操作失败");
    } finally{ 

            try {
                if (preparedStatement!=null){
                preparedStatement.close();}
                if (conn != null){
                conn.close();}
            } catch (SQLException ex) {
               System.out.println("close failed");
            }
        }



    }


}

public class HRActionListener implements ActionListener{


public void actionPerformed(ActionEvent e ) {
     String string = e.getActionCommand();
     if(string.equals("添加")){
         new AddGui().ShowAddGui();
     }}}

public class AddGui {
public void ShowAddGui() {
JFrame frame = new JFrame("添加");
Container con = new Container();
con.setLayout(new FlowLayout());

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(6,2));

JLabel label = new JLabel("添加新员工");
label.setHorizontalAlignment(JLabel.CENTER);
con.add(label);

JButton id = new JButton("工号:");
JTextField idField = new JTextField(5);
panel.add(id); 
panel.add(idField);

JButton name = new JButton("姓名:");
JTextField nameField = new JTextField(5);
panel.add(name);
panel.add(nameField);

JButton sex = new JButton("性别:");
JTextField sexField = new JTextField(5);
panel.add(sex);
panel.add(sexField);

JButton depart = new JButton("部门:");
JTextField departField = new JTextField(5);
panel.add(depart);
panel.add(departField);

JButton salary = new JButton("工资:");
JTextField salaryField = new JTextField(5);
panel.add(salary);
panel.add(salaryField);


JButton addTo = new JButton("确定");
addTo.addActionListener(new HRActionListener());

JButton deleteTo = new JButton("取消");
panel.add(addTo);
panel.add(deleteTo);

con.add(panel);
frame.add(con);
frame.setVisible(true);
frame.setSize(220, 270);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}

1 个答案:

答案 0 :(得分:1)

您可以尝试让AddGui班级实施ActionListener

class AddGui implements ActionListener {

    private JTextField idField = new JTextField(5);
    //Other input fields

    @Override
    public void actionPerformed(ActionEvent e) {
        //Get all other fields here
        Insert record = new Insert(Integer.parseInt(idField.getText()), "name", "sex", "dept", 0);
        //call to insert()
    }

在这种情况下,您的按钮声明应为:

JButton addTo = new JButton("");
addTo.addActionListener(this);