观察者模式实例与解析--实例二:自定义登录控件

Java事件处理模型中应用了观察者模式,下面通过一个实例来学习如何自定义Java控件,并给该控件增加相应的事件。该实例基于Java Swing/AWT控件,在Swing/AWT的相关类中封装了对事件的底层处理。 

 

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

//Concrete Subject
public class LoginBean extends JPanel implements ActionListener
{
	private JLabel labUserName,labPassword;
	private JTextField txtUserName;
	private JPasswordField txtPassword;
	private JButton btnLogin,btnClear;
	
	private LoginEventListener lel;  //Abstract Observer
	
	private LoginEvent le;
	
	public LoginBean()
	{
		this.setLayout(new GridLayout(3,2));
		labUserName=new JLabel("User Name:");
		add(labUserName);
		
		txtUserName=new JTextField(20);
		add(txtUserName);
		
		labPassword=new JLabel("Password:");
		add(labPassword);
		
		txtPassword=new JPasswordField(20);
		add(txtPassword);
		
		btnLogin=new JButton("Login");
		add(btnLogin);
		
		btnClear=new JButton("Clear");
		add(btnClear);
		
		btnClear.addActionListener(this);
		btnLogin.addActionListener(this);//As a concrete observer for another subject,ActionListener as the abstract observer.
	}
	
	//Add an observer.
	public void addLoginEventListener(LoginEventListener lel)
	{
		this.lel=lel;
	}
	
	//private or protected as the notify method
	private void fireLoginEvent(Object object,String userName,String password)
	{
		le=new LoginEvent(btnLogin,userName,password);
		lel.validateLogin(le);
	}
	
	public void actionPerformed(ActionEvent event)
	{
		if(btnLogin==event.getSource())
		{
			String userName=this.txtUserName.getText();
			String password=this.txtPassword.getText();
			
			fireLoginEvent(btnLogin,userName,password);
		}
		if(btnClear==event.getSource())
		{
			this.txtUserName.setText("");			
			this.txtPassword.setText("");
		}
	}
}

 

 

import java.util.EventObject;

public class LoginEvent extends EventObject
{
	private String userName;
	private String password;
	public LoginEvent(Object source,String userName,String password)
	{
		super(source);
		this.userName=userName;
		this.password=password;
	}
	public void setUserName(String userName)
	{
		this.userName=userName;
	}
	public String getUserName()
	{
		return this.userName;
	}
	public void setPassword(String password)
	{
		this.password=password;
	}
	public String getPassword()
	{
		return this.password;
	}
}
import java.util.EventListener;

//Abstract Observer
public interface LoginEventListener extends EventListener
{
	public void validateLogin(LoginEvent event);
}

import javax.swing.*;
import java.awt.*;

//Concrete Observer
public class LoginValidatorA extends JFrame implements LoginEventListener
{
	private JPanel p;
	private LoginBean lb;
	private JLabel lblLogo;
	public LoginValidatorA()
	{
		super("Bank of China");
		p=new JPanel();
		this.getContentPane().add(p);
		lb=new LoginBean();
		lb.addLoginEventListener(this);
		
		Font f=new Font("Times New Roman",Font.BOLD,30);
		lblLogo=new JLabel("Bank of China");
		lblLogo.setFont(f);
		lblLogo.setForeground(Color.red);
		
		p.setLayout(new GridLayout(2,1));
		p.add(lblLogo);
		p.add(lb);
		p.setBackground(Color.pink);
		this.setSize(600,200);
		this.setVisible(true);
	}
	public void validateLogin(LoginEvent event)
	{
		String userName=event.getUserName();
		String password=event.getPassword();
		
		if(0==userName.trim().length()||0==password.trim().length())
		{
			JOptionPane.showMessageDialog(this,new String("Username or Password is empty!"),"alert",JOptionPane.ERROR_MESSAGE);
		}
		else
		{
			JOptionPane.showMessageDialog(this,new String("Valid Login Info!"),"alert",JOptionPane.INFORMATION_MESSAGE);			
		}
	}
	public static void main(String args[])
	{
		new LoginValidatorA().setVisible(true);
	}
}

import javax.swing.*;
import java.awt.*;

public class LoginValidatorB extends JFrame implements LoginEventListener
{
	private JPanel p;
	private LoginBean lb;
	private JLabel lblLogo;
	public LoginValidatorB()
	{
		super("China Mobile");
		p=new JPanel();
		this.getContentPane().add(p);
		lb=new LoginBean();
		lb.addLoginEventListener(this);
		
		Font f=new Font("Times New Roman",Font.BOLD,30);
		lblLogo=new JLabel("China Mobile");
		lblLogo.setFont(f);
		lblLogo.setForeground(Color.blue);
		
		p.setLayout(new GridLayout(2,1));
		p.add(lblLogo);
		p.add(lb);
		p.setBackground(new Color(163,185,255));
		this.setSize(600,200);
		this.setVisible(true);
	}
	public void validateLogin(LoginEvent event)
	{
		String userName=event.getUserName();
		String password=event.getPassword();
		
		if(userName.equals(password))
		{
			JOptionPane.showMessageDialog(this,new String("Username must be different from password!"),"alert",JOptionPane.ERROR_MESSAGE);
		}
		else
		{
			JOptionPane.showMessageDialog(this,new String("Rigth details!"),"alert",JOptionPane.INFORMATION_MESSAGE);			
		}
	}
	public static void main(String args[])
	{
		new LoginValidatorB().setVisible(true);
	}
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值