[英]pass value from JTextField to ActionListener
I am writing a small GUI program for human resource management, class Insert is to add person to MySQL when button "submit" is clicked, and I have problem to pass the value read from JTextField
to HRActionListener
which is implemented from ActionListener
, which allows only one parameter, I am trying to include all the buttons event in one class. 我正在编写一个用于人力资源管理的小型GUI程序,插入类是在单击按钮“ submit”时将人员添加到MySQL,并且我无法将从JTextField
读取的值传递给HRActionListener
,后者是从ActionListener
实现的,仅允许一个参数,我试图将所有按钮事件都包含在一个类中。
here are the code for Insert ,
AddGui and
HRActionListener` 这是Insert ,
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);}}
You can try and make your AddGui
class implement your ActionListener
您可以尝试使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()
}
In this case, your button declaration should be: 在这种情况下,您的按钮声明应为:
JButton addTo = new JButton("");
addTo.addActionListener(this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.