-2

我正在尝试在 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();

        }
    });
}

但是当textLoginUsertextLoginPass的打印值为空时。

我希望我已经解释并提前感谢。

编辑:

@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....
4

2 回答 2

2

永远不会设置文本字段。您应该以其他方式获取内容:

btnIngresar = new JButton("Ingresar");
btnIngresar.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        au = new AccionesUsuarios();
        au.iniciarSesion(textLoginUser, textLoginPass);

    }
});

AccionesUsuarios.java

public void iniciarSesion(String user, String pass){
    dataLogin = new iniciarSesionw();
    String usr = "", pwd = "";
    usr = user.trim();
    pwd = pass.trim();

    System.out.println("Usuario: "+usr);
    System.out.println("Contraseña: "+pwd);
/*try {
        conn.openConnection();

        /*dataLogin = new iniciarSesionw();
        String username = usr;
        String password = pwd;

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

正如 Capn Sparrow 指出的那样,您在 AccionesUsuarios.java 中创建了 iniciarSesionw-Class 的新实例,这会导致空值;)。

于 2012-11-21T09:40:19.090 回答
0

在 AccionesUsuarios 中执行此操作时:

dataLogin = new iniciarSesionw();

您正在创建 iniciarSesionw 类的新实例。因为它是一个不同的实例,所以它不会具有在另一个实例中设置的任何值。

相反,您可以尝试将引用传递给您的 iniciarSesionw 实例,如下所示:

AccionesUsuarios.java

public class AccionesUsuarios {

  private static BaseDatos conn;
  private iniciarSesionw dataLogin;  // note, I've removed the static keyword

  public AccionesUsuarios(iniciarSesionw dataLogin) {
    this.dataLogin = dataLogin;
  }

  ...

iniciarSessionw.java

    public void actionPerformed(ActionEvent arg0) {
        au = new AccionesUsuarios( this );
于 2012-11-21T09:36:27.743 回答