在Java NetBeans中,按下EnterPad不起提交按钮的作用



我正在尝试使用回车键作为提交按钮,但无法正常工作。我的意思是,每当我试图点击回车键时,都不会向DB提交任何数据。我的代码如下:

public class Signup extends javax.swing.JFrame implements KeyListener {
@Override
public void keyPressed(KeyEvent ke) {
// create function for key event for user registration sign up using enter keypad
String fname = jTextField_name.getText();
String sname = jTextField_surname.getText();
String nil = jTextField_nin.getText();
String loc = jTextField_location.getText();
String pos = jTextField_pos.getText();
String agentno = jTextField_agentno.getText();
String password = String.valueOf(jPasswordField_password.getPassword());
String hashpassword = BCrypt.hashpw(password, BCrypt.gensalt(30));
if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
if (verifyField()) {
if (!checkPos(pos)) {
PreparedStatement ps;
ResultSet rs;
String registerUserQuery = "INSERT INTO `user`( `name`, `surname`, `nin`, `location`, `pos`, `agent_no`, `password`) VALUES (?,?,?,?,?,?,?)";
try {
ps = My_Connect.getconnection().prepareStatement(registerUserQuery);
ps.setString(1, fname);
ps.setString(2, sname);
ps.setString(3, nil);
ps.setString(4, loc);
ps.setString(5, pos);
ps.setString(6, agentno);
ps.setString(7, hashpassword);
if (ps.executeUpdate() != 0) {
JOptionPane.showMessageDialog(null, "Your Account Has Been Created Suuccessully");
} else {
JOptionPane.showMessageDialog(null, "Error :Check your Information ");
}
} catch (SQLException ex) {
Logger.getLogger(Signup.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
jButton_signup.doClick();
}
}
// create a function to verify the empty fields
public boolean verifyField(){
String fname = jTextField_name.getText();
String sname = jTextField_surname.getText();
String nil= jTextField_nin.getText();
String loc= jTextField_location.getText();
String pos = jTextField_pos.getText();
String pass1 = String.valueOf(jPasswordField_password.getPassword());
String passc1 = String.valueOf(jPasswordField_confirmpassword.getPassword());

if(fname.trim().equals("") || sname.trim().equals("")||nil.trim().equals("")|| loc.trim().equals("") || pos .trim().equals("") || passc1.trim().equals(""))
{
JOptionPane.showMessageDialog(null,"One or more field are Empty","Empty Field",2);
return false;
} 
// check if the two password are equals
else if(!pass1.equals(passc1))
{
JOptionPane.showMessageDialog(null,"Password Doesn't Match","Confirm Password",2);
return false;
}
// If the everything is ok
else
{
return true;
}
}

// create a unaction to check if the entired username already exit in the database
public boolean checkPos(String pos){

PreparedStatement st;
ResultSet rs;  
boolean pos_exist = false;
String query = "SELECT * FROM `user` WHERE `pos`= ? ";

try {
st = My_Connect.getconnection().prepareStatement(query);
st.setString(1, pos);
rs = st.executeQuery();
if(rs.next()){
pos_exist = true;
JOptionPane.showMessageDialog(null,"This pos is already taken, Choose another one");
}
} catch (SQLException ex) {
Logger.getLogger(Signup.class.getName()).log(Level.SEVERE, null, ex);
}
return pos_exist;
} 
public boolean checkAgent(String agent){

PreparedStatement st;
ResultSet rs;  
boolean agent_exist = false;
String query = "SELECT * FROM `user` WHERE `agent_no`= ? ";

try {
st = My_Connect.getconnection().prepareStatement(query);
st.setString(1, agent);
rs = st.executeQuery();
if(rs.next()){
agent_exist = true;
JOptionPane.showMessageDialog(null,"This agent  is already taken, Choose another one");
}
} catch (SQLException ex) {
Logger.getLogger(Signup.class.getName()).log(Level.SEVERE, null, ex);
}
return agent_exist;
} 

public boolean checkNin(String nin){

PreparedStatement st;
ResultSet rs;  
boolean nin_exist = false;
String query = "SELECT * FROM `user` WHERE `nin`= ? ";

try {
st = My_Connect.getconnection().prepareStatement(query);
st.setString(1, nin);
rs = st.executeQuery();
if(rs.next()){
nin_exist = true;
JOptionPane.showMessageDialog(null,"This nin is already taken, Check you NIN ");
}
} catch (SQLException ex) {
Logger.getLogger(Signup.class.getName()).log(Level.SEVERE, null, ex);
}
return nin_exist;
} 
/**
* @param args the command line arguments
*/
/**
*
* @param args the command line arguments
* @throws java.lang.ClassNotFoundException
*/
/**
*
* @param args the command line arguments
* @throws java.lang.ClassNotFoundException
* @throws java.lang.InstantiationException
*/
/**
*
* @param args the command line arguments
* @throws java.lang.ClassNotFoundException
* @throws java.lang.InstantiationException
* @throws java.lang.IllegalAccessException
*/
/**
*
* @param args the command line arguments
* @throws java.lang.ClassNotFoundException
* @throws java.lang.InstantiationException
* @throws java.lang.IllegalAccessException
* @throws javax.swing.UnsupportedLookAndFeelException
*/
public static void main(String args[])  throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(LottoryPaymentType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(LottoryPaymentType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(LottoryPaymentType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(LottoryPaymentType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
try {
//here you can put the selected theme class name in JTattoo
UIManager.setLookAndFeel("com.jtattoo.plaf.aluminium.AluminiumLookAndFeel");
// UIManager.setLookAndFeel ("com.jtattoo.plaf.aero.AeroLookAndFeel ");

} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Login .class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Signup().setVisible(true);
}
});
}

几年来我也遇到过同样的问题。如果我记得正确的话,问题是KeyListener只有在对象具有焦点时才能工作。

如果添加:

frame.requestFocus();

对于其他组件:

button.setFocusable(false);

但我认为你正在寻找:KeyBinding!