/*
* LoginEvent.java
*/
package beanTest;
import java.util.EventObject;
public class LoginEvent extends EventObject {
private static final long serialVersionUID = 1L;
private String userName;
private String password;
public LoginEvent(Object o, String userName, String password) {
super(o);
this.userName = userName;
this.password = password;
}
public String getPassword() {
return password;
}
public String getUserName() {
return userName;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
/**
* LoginListener.java
*/
package beanTest;
import java.util.EventListener;
/**
* @author wuliqiang
*
*/
public interface LoginListener extends EventListener {
public void validateLogin(LoginEvent e);
}
/**
* LoginBean.java
*/
package beanTest;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
* @author wuliqiang
*
*/
public class LoginBean extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private JPasswordField textPassword;
private JTextField textUsername;
private JButton loginButton;
private LoginListener loginListener;
private LoginEvent loginEvent;
public LoginBean() {
super();
setLayout(null);
final JLabel loginNameLabel = new JLabel();
loginNameLabel.setText("Login Name:");
loginNameLabel.setBounds(82, 44, 70, 18);
add(loginNameLabel);
textUsername = new JTextField();
textUsername.setBounds(176, 42, 108, 22);
add(textUsername);
final JLabel passwordLabel = new JLabel();
passwordLabel.setText("Password:");
passwordLabel.setBounds(82, 94, 70, 18);
add(passwordLabel);
textPassword = new JPasswordField();
textPassword.setBounds(176, 92, 108, 22);
add(textPassword);
loginButton = new JButton();
loginButton.setText("Login");
loginButton.setBounds(140, 140, 77, 28);
loginButton.addActionListener(this);
add(loginButton);
}
public void actionPerformed(ActionEvent e) {
// 初始化 loginEvent
loginEvent = new LoginEvent(loginButton, textUsername.getText(),
new String(textPassword.getPassword()));
fireLoginEvent(loginEvent);
}
public void addLoginListener(LoginListener loginListener) {
// 负责初始化 loginListener;
this.loginListener = loginListener;
}
public void fireLoginEvent(LoginEvent loginEvent) {
// 调用接口的方法
loginListener.validateLogin(loginEvent);
}
}