首页 > 解决方案 > 如何保存在登录对话框中输入的凭据并在另一个类中访问它?

问题描述

我有两个班和一个主班。在一个类 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

标签: javaswingactionevent

解决方案


推荐阅读