gpt4 book ai didi

java - java中将对象传递给另一个类

转载 作者:行者123 更新时间:2023-12-02 06:24:13 27 4
gpt4 key购买 nike

我有我的主类,我们称之为 MainGUI它包含以下内容

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
UserUI aGui = new UserUI(user);
}

用户对象有用户名和密码我需要将其传递给 UserUI 并在文本区域中显示用户名和密码到目前为止,我的 UserUI 文本区域中没有出现任何内容,UserUI 类如下所示:

package Details;

public class UserUI extends javax.swing.JFrame {

User user;

public UserUI() {
initComponents();

}


public void setUser(User user){

if(user.getName().equals("Bob")){
jTextArea1.setText(user.getName());
}
}

UserUI(User user) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
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(UserUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(UserUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(UserUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(UserUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new UserUI().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}

最佳答案

UserUI(User user)中调用this()

UserUI(User user) {
this();
this.user = user;
setUser(user);
}

关于java - java中将对象传递给另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20723476/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com