java - 如何保存在登录对话框中输入的凭据并在另一个类中访问它?
问题描述
我有两个班和一个主班。在一个类 LoginDialog 中,我有一个方法 actionPerformed(),它会提示用户在其中输入凭据的登录对话框。该方法还包括一个 ActionEvent 参数。如何保存这些凭据,以便我可以在我的其他课程 Transfer 中调用/访问它。我在 LoginDialog 类中包含了一个 getUser() 方法,该方法调用 actionPerformed() 并尝试打印用户并在另一个类传输中调用 getUser() 方法,但我不断收到 nullpointerexception。
主要的:
public static void main(String[] args) {
Transfer transfer = new Transfer();
JFrame parentFrame = new JFrame("Transfer Login");
parentFrame.setBounds(400, 400, 400, 400);
// Create and set up the window.
JDialog loginDialog = new JDialog(parentFrame, "Transfer Login", Dialog.ModalityType.DOCUMENT_MODAL);
loginDialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
// Create and set up the content pane.
final LoginDialog newContentPane = new LoginDialog(loginDialog, bltFrame);
Container c1 = newContentPane.getContentPane();
loginDialog.setContentPane(c1);
// Make sure the focus goes to the right component
// whenever the frame is initially given the focus.
newContentPane.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
newContentPane.resetFocus();
}
public void windowClosed(WindowEvent e) {
System.out.println("jdialog window closed");
}
});
loginDialog.pack();
// This will hang until the dialog is closed
loginDialog.setVisible(true);
newContentPane.getUser(e);
transfer.print();
}
登录对话框:
public class LoginDialog extends JDialog implements ActionListener {
public String user;
public char [] pw;
private String OK = "ok";
private String HELP = "help";
private JFrame controllingFrame; // needed for dialogs
private JTextField userField;
private JPasswordField passwordField;
private TransferService transferService;
BaselineTransferFrame btf;
public LoginDialog(JDialog loginDialog, BaselineTransferFrame bltFrame) {
// Use the default FlowLayout.
controllingFrame = (JFrame) loginDialog.getParent();
transferService = bltFrame.getTransferService();
btf = bltFrame;
// Create everything.
userField = new JTextField(12);
passwordField = new JPasswordField(20);
passwordField.setActionCommand(OK);
passwordField.addActionListener(this);
JLabel uLabel = new JLabel("Enter your Dimensions User ID: ");
JLabel pLabel = new JLabel("Enter password: ");
uLabel.setLabelFor(userField);
pLabel.setLabelFor(passwordField);
// Lay out everything.
JPanel textPane = new JPanel(new GridLayout(3, 1));
textPane.add(uLabel);
textPane.add(userField);
textPane.add(pLabel);
textPane.add(passwordField);
textPane.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(10, 10, 10, 10), new EtchedBorder()));
JComponent buttonPane = createButtonPanel(textPane);
add(textPane);
add(buttonPane);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (OK.equals(cmd)) { // Process the password.
user = userField.getText();
pw = passwordField.getPassword();
HashMap<String, String> connectParms = transferService.getConnectionParms("a");
DimensionsSession dmSession = null;
try {
dmSession = transferService.connectToDimensions(connectParms.get("server"), connectParms.get("connection"), connectParms.get("database"), user, String.valueOf(pw));
System.out.println("Here");
} catch (BaselineTransferException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (dmSession != null) {
JOptionPane.showMessageDialog(controllingFrame, "Success! You typed the right password.");
dmSession.close();
controllingFrame.dispose();
} else {
JOptionPane.showMessageDialog(controllingFrame, "Invalid password. Try again.", "Error Message",
JOptionPane.ERROR_MESSAGE);
}
// Zero out the possible password, for security.
Arrays.fill(pw, '0');
passwordField.selectAll();
resetFocus();
} else { // The user has asked for help.
JOptionPane.showMessageDialog(controllingFrame,
"You can get the password by searching this example's\n"
+ "source code for the string \"correctPassword\".\n"
+ "Or look at the section How to Use Password Fields in\n"
+ "the components section of The Java Tutorial.");
}
}
public void getUser(ActionEvent e){
actionPerformed(e);
System.out.println(user);
}
转移:
public void print(){
ActionEvent e = null;
LoginDialog user1 = new LoginDialog(loginDialog, bltFrame);
user1.getUser(e);
}
输出:
Caused by: java.lang.NullPointerException
at com.northgrum.ssee.gui.LoginDialog.getUser(LoginDialog(The line with String cmd = e.getActionCommand(); in the method actionPerformed()))
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)
... 13 more
解决方案
推荐阅读
- arrays - C 中的数组 - 左值与右值
- python - 我正在用 Python 准备一个代码,我在某一时刻感到震惊。我想明智地展示我最喜欢的食物列,但它显示的是行明智的。需要建议
- database - 数据库导入 csv 文件错误:导入数据时出错:UNIQUE 约束失败:StockPrice.stock_symbol
- bash - 获取文件完整路径中的最后一个子文件夹名称
- python - 如何从其他数据框(Python Pandas)中的非重复项中获取新的数据框?
- google-analytics - 识别登录页面(下一个会话)> 退出页面(上一个会话)时与 Bigquery 和 GA(通用)的巨大差异
- java - 如何通过应用程序上下文获取注释为服务的接口实例
- reverse-proxy - 定义 HAProxy ACL 以根据请求 URL 路由后端
- python - Django 迭代 zip 中的所有值
- java - 如何从 Ant 生成的战争中排除 java 源文件?