Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5520462
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: Java

2008-09-18 16:42:39

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

}

/**
 * MainFrame.java
 */

package beanTest;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * @author wuliqiang
 *
 */

public class MainFrame extends JFrame implements LoginListener {

    private static final long serialVersionUID = 1L;

    private LoginBean loginBean;

    public MainFrame() {
        super("my test MainFrame");
        loginBean = new LoginBean();
        loginBean.addLoginListener(this);

        getContentPane().add(loginBean);
        setSize(400, 500);
        setVisible(true);
    }

    public static void main(String[] args) {
        MainFrame app = new MainFrame();
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void validateLogin(LoginEvent e) {

        JOptionPane.showMessageDialog(this, "userName:" + e.getUserName()
                + "\n" + "Password:" + new String(e.getPassword()));

    }

}

 

运行可以通过,总算大功告成了....

阅读(1168) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~