Hi all
I try to copy paste from a site java example ,there are 3 files,Main.java,Login.java,and LoginDialog.java.
I can compile Login.java ,but when I compile LoginDialog.java,it said that it cannot find symbol Login.authenticate (a method in Login.java)
I have check that there is this Login.authenticate method
to make clearer I attach Login.java,and LoginDialog.java below
the source site is http://www.javaswing.org/jdialog-login-dialog-example.aspx
Hope that someone with more experience can teach me where to fix.
I place source code and class in c:\java\jdialogdemo
thank you
denny
///----------------------------------------
package jdialogdemo;
public class Login {
public static boolean authenticate(String username, String password) {
// hardcoded username and password
if (username.equals("bob") && password.equals("secret")) {
return true;
}
return false;
}
}
//---------------------------------
package jdialogdemo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class LoginDialog extends JDialog {
private JTextField tfUsername;
private JPasswordField pfPassword;
private JLabel lbUsername;
private JLabel lbPassword;
private JButton btnLogin;
private JButton btnCancel;
private boolean succeeded;
public LoginDialog(Frame parent) {
super(parent, "Login", true);
//
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints cs = new GridBagConstraints();
cs.fill = GridBagConstraints.HORIZONTAL;
lbUsername = new JLabel("Username: ");
cs.gridx = 0;
cs.gridy = 0;
cs.gridwidth = 1;
panel.add(lbUsername, cs);
tfUsername = new JTextField(20);
cs.gridx = 1;
cs.gridy = 0;
cs.gridwidth = 2;
panel.add(tfUsername, cs);
lbPassword = new JLabel("Password: ");
cs.gridx = 0;
cs.gridy = 1;
cs.gridwidth = 1;
panel.add(lbPassword, cs);
pfPassword = new JPasswordField(20);
cs.gridx = 1;
cs.gridy = 1;
cs.gridwidth = 2;
panel.add(pfPassword, cs);
panel.setBorder(new LineBorder(Color.GRAY));
btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (Login.authenticate(getUsername(), getPassword())) {
JOptionPane.showMessageDialog(LoginDialog.this,
"Hi " + getUsername() + "! You have successfully logged in.",
"Login",
JOptionPane.INFORMATION_MESSAGE);
succeeded = true;
dispose();
} else {
JOptionPane.showMessageDialog(LoginDialog.this,
"Invalid username or password",
"Login",
JOptionPane.ERROR_MESSAGE);
// reset username and password
tfUsername.setText("");
pfPassword.setText("");
succeeded = false;
}
}
});
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
JPanel bp = new JPanel();
bp.add(btnLogin);
bp.add(btnCancel);
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().add(bp, BorderLayout.PAGE_END);
pack();
setResizable(false);
setLocationRelativeTo(parent);
}
public String getUsername() {
return tfUsername.getText().trim();
}
public String getPassword() {
return new String(pfPassword.getPassword());
}
public boolean isSucceeded() {
return succeeded;
}
}