gpt4 book ai didi

java - 将值从 JTextField 传递到 ActionListener

转载 作者:行者123 更新时间:2023-12-01 12:46:20 24 4
gpt4 key购买 nike

我正在编写一个用于人力资源管理的小型GUI程序,类Insert是在单击“提交”按钮时将人员添加到MySQL,并且我在将从JTextField读取的值传递到HRActionListener 是从 ActionListener 实现的,它只允许一个参数,我试图将所有按钮事件包含在一个类中。

这里是 InsertAddGuiHRActionListener` 的代码

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);}}

最佳答案

您可以尝试让您的 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);

关于java - 将值从 JTextField 传递到 ActionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24671160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com