getter不会在另一个类java中返回值

时间:2019-02-17 16:45:04

标签: java

我的头等舱

 * public class loginForm extends javax.swing.JFrame {
    public loginForm() {
    initComponents();


}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")


private void usernameActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
}                                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    validateLogin validateLogin = new validateLogin();

    validateLogin.validLogin();

    JOptionPane.showMessageDialog(null,getUsername());

}                                        


/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* 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(loginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(loginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(loginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(loginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(() -> {
        new loginForm().setVisible(true);
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPasswordField password;
private javax.swing.JTextField username;
// End of variables declaration                   

public String getPassword() {
    return Arrays.toString(password.getPassword());
}

public void setUsername(String user) {
    this.username.setText(user); 
}

public String getUsername() {
    return username.getText();
}}

我正在尝试访问它们的第二堂课。

 public class validateLogin {   

 public validateLogin() {
    System.out.println("In ValidLogin Constructor");

}


public void validLogin(){

    loginForm loginForm = new loginForm();
        System.out.println(loginForm.getUsername()); //This value is not printing in the function. 

}
//I had tried this because i thought main class is needed but still no output.

}

问题在于它不是在获取值。它不输入有效的登录功能。 没有错误。 我试图实现的是将逻辑隔离在另一个类中,以便以后进行编辑很容易。 请引导我我在哪里弄错了。

注意: 1.它进入另一个类的构造函数。它打印打印语句。 2.我在GUI本身中设置值,当我在button_clicked事件上调用它时,此函数将检索该值。

1 个答案:

答案 0 :(得分:0)

您没有在任何地方设置用户名的值。这就是为什么loginForm.getusername()返回空字符串的原因。