gpt4 book ai didi

java - Jdialog 将用户凭据传回其包含的 JFrame?或者,JFrame 从其 JDialog 获取凭据?

转载 作者:行者123 更新时间:2023-12-01 22:45:04 25 4
gpt4 key购买 nike

如何将用户凭据传回到包含的 JFrame,以便 JFrame 知道特定用户? JFrame 有一个 main 方法。

包含的 JFrame 能否以某种方式从 Dialog 中获取用户?

jbtOk actionListener找到可接受的User时,然后...?

package legacy.gui.general;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import legacy.database.User;
import legacy.database.Users;

public class LoginDialog extends javax.swing.JDialog {

private static final Logger log = Logger.getLogger(LoginDialog.class.getName());
private final JLabel jlblUsername = new JLabel("Username");
private final JLabel jlblPassword = new JLabel("Password");
private final JTextField jtfUsername = new JTextField(15);
private final JPasswordField jpfPassword = new JPasswordField();
private final JButton jbtOk = new JButton("Login");
private final JButton jbtCancel = new JButton("Cancel");
private final JLabel jlblStatus = new JLabel(" ");

public LoginDialog() {
this(null, true);
}

public LoginDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
log.info("simple constructor..");
initComponents();
}

public LoginDialog(final JFrame parent, boolean modal) {
super(parent, modal);
log.info("complex constructor..");
JPanel p3 = new JPanel(new GridLayout(2, 1));
p3.add(jlblUsername);
p3.add(jlblPassword);
JPanel p4 = new JPanel(new GridLayout(2, 1));
p4.add(jtfUsername);
p4.add(jpfPassword);
JPanel p1 = new JPanel();
p1.add(p3);
p1.add(p4);
JPanel p2 = new JPanel();
p2.add(jbtOk);
p2.add(jbtCancel);
JPanel p5 = new JPanel(new BorderLayout());
p5.add(p2, BorderLayout.CENTER);
p5.add(jlblStatus, BorderLayout.NORTH);
jlblStatus.setForeground(Color.RED);
jlblStatus.setHorizontalAlignment(SwingConstants.CENTER);
setLayout(new BorderLayout());
add(p1, BorderLayout.CENTER);
add(p5, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jbtOk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
List<User> users = new Users().getUsers();
for (User u : users) {
if (Arrays.equals(u.getPassword().toCharArray(), jpfPassword.getPassword())
&& u.getUser().equals(jtfUsername.getText())) {
parent.setVisible(true);
setVisible(false);
} else {
jlblStatus.setText("Invalid username or password");
}
}
}
});
jbtCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
parent.dispose();
System.exit(0);
}
});
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 580, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 389, Short.MAX_VALUE)
);

pack();
}// </editor-fold>

// Variables declaration - do not modify
// End of variables declaration
}

JFrame 创建对话框:

public MainFrame() {
initComponents();
passDialog = new LoginDialog(this, true);
passDialog.setVisible(true);
// setExtendedState(JFrame.MAXIMIZED_BOTH);
}

最佳答案

首先,创建一个名为User的私有(private)实例字段...

private User user;

接下来,更改您的Login ActionListener来存储匹配的用户...

jbtOk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
List<User> users = new Users().getUsers();
for (User u : users) {
if (Arrays.equals(u.getPassword().toCharArray(), jpfPassword.getPassword())
&& u.getUser().equals(jtfUsername.getText())) {
user = u;
dispose();
break;
} else {
jlblStatus.setText("Invalid username or password");
}
}
}
});

接下来,在 LoginDialog 中创建一个方法,以允许调用者获取 User 值...

public User getUser() {
return user;
}

接下来,当LoginDialog关闭时,获取User...

public MainFrame() {
initComponents();
passDialog = new LoginDialog(this, true);
passDialog.setVisible(true);
User user = passDialog.getUser();
}

旁注

  • 不要尝试通过 LoginDialog 控制调用者。例如,如果用户选择“取消”,则登录对话框不负责确定应该发生什么情况。 LoginDialog 应该简单地完成它的契约(Contract)工作(收集用户详细信息并验证凭据),并将其余的工作留给调用者...

关于java - Jdialog 将用户凭据传回其包含的 JFrame?或者,JFrame 从其 JDialog 获取凭据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25484477/

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