Java:为什么我无法从另一个类的jTextFields获取值?

时间:2012-11-21 09:26:47

标签: java class textfield

我尝试在Java中构建基于“MVC”的应用程序,我有2个类: 1:功能和动作 2:按钮,摆动元素

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

iniciarSesionw.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....

2 个答案:

答案 0 :(得分: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实例,导致空值;)。

答案 1 :(得分: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;
  }

  ...

<强> iniciarSesionw.java

    public void actionPerformed(ActionEvent arg0) {
        au = new AccionesUsuarios( this );