我正在尝试在 Java 中构建基于“MVC”的应用程序,我有 2 个类:1:函数和动作 2:按钮、Swing 元素
我想从 1 类访问到 2 类并执行特定操作。
AccionesUsuarios.java
public class AccionesUsuarios{
private static BaseDatos conn;
private static iniciarSesionw dataLogin;
public void iniciarSesion(){
dataLogin = new iniciarSesionw();
String usr = "", pwd = "";
usr = dataLogin.textLoginUser.getText().trim();
pwd = dataLogin.textLoginPass.getText().trim();
System.out.println("Usuario: "+usr);
System.out.println("Contraseña: "+pwd);
/*try {
conn.openConnection();
/*dataLogin = new iniciarSesionw();
String username = dataLogin.textLoginUser.getText();
String password = dataLogin.textLoginPass.getText();
ComandoBaseDatos comando = new ComandoBaseDatos("SELECT * FROM Usuarios_Operadores WHERE Usuario = '123' AND Clave = '123'");
ResultSet rs = comando.SQLExecute();
while (rs.next()) {
JOptionPane.showMessageDialog(null, "Correcto!");
}
conn.closeConnection();
} catch (SQLException ex) {
conn.getSqlMessage(ex);
}*/
}
}
iniciarSessionw.java
public class iniciarSesionw {
public JTextField textLoginUser;
public JTextField textLoginPass;
public JButton btnIngresar;
private static AccionesUsuarios au;
/*Launch the application */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
iniciarSesionw window = new iniciarSesionw();
window.frmItcuautlaControl.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public iniciarSesionw() {
initialize();
}
private void initialize() {
textLoginUser = new JTextField();
textLoginUser.setBounds(185, 110, 153, 27);
panel.add(textLoginUser);
textLoginUser.setColumns(10);
textLoginPass = new JTextField();
textLoginPass.setColumns(10);
textLoginPass.setBounds(185, 148, 153, 27);
panel.add(textLoginPass);
btnIngresar = new JButton("Ingresar");
btnIngresar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
au = new AccionesUsuarios();
au.iniciarSesion();
}
});
}
但是当textLoginUser和textLoginPass的打印值为空时。
我希望我已经解释并提前感谢。
编辑:
用@looper回答解决了!!但是现在..取消注释 AccionesUsuarios.java 中包含与数据库的连接的部分并显示这些错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Controlador.AccionesUsuarios.iniciarSesion(AccionesUsuarios.java:43)
at Vista.iniciarSesionw$2.actionPerformed(iniciarSesionw.java:124)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
and more....