Как мне вызвать user
и pw
в методе actionPerformed()
, которые равны учетным данным, которые пользователь ввел в диалоговом окне входа в систему, на мою главную страницу, чтобы я мог сослаться на нее или распечатать ее там?
Я пытался настроить некоторые методы получения и установки, но получал NullPointerException
.
класс LoginDialog:
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);
}
protected JComponent createButtonPanel(JPanel p) {
float BTN_SIZE = 16f;
JButton okButton = new JButton("OK");
okButton.setActionCommand(OK);
helpButton.setActionCommand(HELP);
okButton.addActionListener(this);
helpButton.addActionListener(this);
okButton.setFont(okButton.getFont().deriveFont(Font.BOLD, BTN_SIZE));
helpButton.setFont(helpButton.getFont().deriveFont(Font.BOLD, BTN_SIZE));
Boolean enabled = true;
if (enabled) {
okButton.setBackground(new Color(59, 89, 182));
okButton.setEnabled(true);
} else {
okButton.setBackground(new Color(128, 128, 128));
okButton.setEnabled(false);
}
okButton.setForeground(Color.WHITE);
okButton.setFocusPainted(false);
if (enabled) {
helpButton.setBackground(new Color(59, 89, 182));
helpButton.setEnabled(true);
} else {
helpButton.setBackground(new Color(128, 128, 128));
helpButton.setEnabled(false);
}
helpButton.setForeground(Color.WHITE);
helpButton.setFocusPainted(false);
p.add(okButton);
p.add(helpButton);
return p;
}
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.
System.exit(0);
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.");
}
}