/** * 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); }
}
|