首页 > 代码库 > java 编程思想 22.11: java bean 案例代码

java 编程思想 22.11: java bean 案例代码

java 编程思想  22.11:   java bean 案例代码 

thinking in java 4免费下载:http://download.csdn.net/detail/liangrui1988/7580155

package org.rui.swing.bean;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;

import org.rui.classts.Pet;
/**
 * 简单的bean
 * @author lenovo
 *
 */
public class Frog {
	private int jumps;
	private Color color;
	private Pet pet;
	private boolean jmpr;

	public int getJumps() {
		return jumps;
	}

	public void setJumps(int jumps) {
		this.jumps = jumps;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public Pet getPet() {
		return pet;
	}

	public void setPet(Pet pet) {
		this.pet = pet;
	}

	public boolean isJmpr() {
		return jmpr;
	}

	public void setJmpr(boolean jmpr) {
		this.jmpr = jmpr;
	}

	public void addActionListener(ActionListener l){
		//.....
	}
	public void removeActionListener(ActionListener l){
		//.....
	}
	public void addKeyListener(KeyListener l){
		//.....
	}
	public void removeKeyListener(KeyListener l){
		//.....
	}
	//an ordinary public method
	public void croak(){
		System.out.println("Ribbet!");
	}
}


package org.rui.swing.bean;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.BeanInfo;
import java.beans.EventSetDescriptor;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.rui.swing.SwingConsole;
/**
 * 使用introspector抽取BeanInfo
 * @author lenovo
 *
 */
public class BeanDumper extends JFrame {
	private JTextField query = new JTextField(20);
	private JTextArea results = new JTextArea();

	public void print(String s) {
		results.append(s + " \n");
	}

	public void dump(Class<?> bean) {
		results.setText("");
		BeanInfo bi = null;

		try {
			bi = Introspector.getBeanInfo(bean, Object.class);
		} catch (IntrospectionException e) {
			System.out.println("Couldn 't introspect" + bean.getName());
			return;
		}
		// 获取 bean属性 方法
		for (PropertyDescriptor d : bi.getPropertyDescriptors()) {
			Class<?> p = d.getPropertyType();
			if (p == null)
				continue;
			System.out.println("Property type:\n" + p.getName());
			Method m = d.getReadMethod();
			if (m != null)
				System.out.println("read method:+\n" + m.getName());
			Method rm = d.getWriteMethod();
			if (rm != null)
				System.out.println("write method:+\n" + rm.getName());
			System.out.println("====================================");
		}
		System.out.println("public methods:");
		for (MethodDescriptor ms : bi.getMethodDescriptors()) {
			System.out.println("ms:" + ms.getMethod().toString());
		}
		System.out.println("====================================");
		System.out.println("envent support:");
		for (EventSetDescriptor e : bi.getEventSetDescriptors()) {
			System.out.println("listener type:\n"
					+ e.getListenerType().getName());
			for (Method lm : e.getListenerMethods())
				System.out.println("listener method:\n" + lm.getName());

			for (MethodDescriptor lmd : e.getListenerMethodDescriptors())
				System.out.println("listener methodDescriptor:\n"
						+ lmd.getName());
			//
			Method addListener = e.getAddListenerMethod();
			System.out.println("add listener method:\n" + addListener);
			Method removeListener = e.getRemoveListenerMethod();
			System.out.println("Remove Listener Method:\n" + removeListener);
			System.out.println("===========================================");
		}
	}

	// ------------------------------------------------
	class Dumper implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String name = query.getText();
			System.out.println("name=========>"+name);
			Class<?> c = null;
			try {
				c = Class.forName(name);
			} catch (ClassNotFoundException e1) {
				results.setText("couldn 't find " + name);
				e1.printStackTrace();
				return;
			}
			dump(c);
		}
	}

	public BeanDumper() {
		JPanel p = new JPanel();
		p.setLayout(new FlowLayout());
		p.add(new JLabel("qualifeied bean name:"));
		p.add(query);
		add(BorderLayout.NORTH, p);
		add(new JScrollPane(results));
		Dumper dmpr = new Dumper();
		query.addActionListener(dmpr);
		query.setText("org.rui.swing.bean.Frog");
		dmpr.actionPerformed(new ActionEvent(dmpr, 0, ""));
	}

	public static void main(String[] args) {
		//工具类
		SwingConsole.run(new BeanDumper(), 600, 500);
	}
}

/**outputt:
 name=========>org.rui.swing.bean.Frog
Property type:
java.awt.Color
read method:+
getColor
write method:+
setColor
====================================
Property type:
boolean
read method:+
isJmpr
write method:+
setJmpr
====================================
Property type:
int
read method:+
getJumps
write method:+
setJumps
====================================
Property type:
org.rui.classts.Pet
read method:+
getPet
write method:+
setPet
====================================
public methods:
ms:public void org.rui.swing.bean.Frog.croak()
ms:public void org.rui.swing.bean.Frog.removeActionListener(java.awt.event.ActionListener)
ms:public void org.rui.swing.bean.Frog.setColor(java.awt.Color)
ms:public int org.rui.swing.bean.Frog.getJumps()
ms:public void org.rui.swing.bean.Frog.addKeyListener(java.awt.event.KeyListener)
ms:public void org.rui.swing.bean.Frog.setJmpr(boolean)
ms:public void org.rui.swing.bean.Frog.setPet(org.rui.classts.Pet)
ms:public void org.rui.swing.bean.Frog.addActionListener(java.awt.event.ActionListener)
ms:public java.awt.Color org.rui.swing.bean.Frog.getColor()
ms:public boolean org.rui.swing.bean.Frog.isJmpr()
ms:public org.rui.classts.Pet org.rui.swing.bean.Frog.getPet()
ms:public void org.rui.swing.bean.Frog.removeKeyListener(java.awt.event.KeyListener)
ms:public void org.rui.swing.bean.Frog.setJumps(int)
====================================
envent support:
listener type:
java.awt.event.ActionListener
listener method:
actionPerformed
listener methodDescriptor:
actionPerformed
add listener method:
public void org.rui.swing.bean.Frog.addActionListener(java.awt.event.ActionListener)
Remove Listener Method:
public void org.rui.swing.bean.Frog.removeActionListener(java.awt.event.ActionListener)
===========================================
listener type:
java.awt.event.KeyListener
listener method:
keyPressed
listener method:
keyReleased
listener method:
keyTyped
listener methodDescriptor:
keyPressed
listener methodDescriptor:
keyReleased
listener methodDescriptor:
keyTyped
add listener method:
public void org.rui.swing.bean.Frog.addKeyListener(java.awt.event.KeyListener)
Remove Listener Method:
public void org.rui.swing.bean.Frog.removeKeyListener(java.awt.event.KeyListener)
===========================================

 */



package org.rui.swing.bean;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.Serializable;
import java.util.TooManyListenersException;

import javax.swing.JPanel;
/**
 * 一个复杂的bean
 * 
 * @author lenovo
 *
 */
public class BangBean extends JPanel implements Serializable {
	private int xm, ym;
	private int cSize = 20;
	private String text = "Bang!";
	private int fontSize = 48;
	private Color tColor = Color.RED;
	private ActionListener actionListener;

	public BangBean() {
		addMouseListener(new ML());
		addMouseMotionListener(new MML());
	}

	public int getcSize() {
		return cSize;
	}

	public void setcSize(int cSize) {
		this.cSize = cSize;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public int getFontSize() {
		return fontSize;
	}

	public void setFontSize(int fontSize) {
		this.fontSize = fontSize;
	}

	public Color gettColor() {
		return tColor;
	}

	public void settColor(Color tColor) {
		this.tColor = tColor;
	}

	// =========================
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(Color.BLACK);
		g.drawOval(xm - cSize / 2, ym - cSize / 2, cSize, cSize);
	}

	// 这是这是一个单播侦听器
	// 最简单形式的侦听器管理
	public void addActionListener(ActionListener l)
			throws TooManyListenersException {
		if (actionListener != null) {
			throw new TooManyListenersException();
		}
		actionListener = l;
	}

	public void removeActionListener(ActionListener l) {
		actionListener = null;
	}

	// ///
	class ML extends MouseAdapter {
		public void mousePressed(MouseEvent e) {
			Graphics g = getGraphics();
			g.setFont(new Font("TimesRoman", Font.BOLD, fontSize));
			int width=g.getFontMetrics().stringWidth(text);
			g.drawString(text, (getSize().width - width) / 2, 
					getSize().height / 2);
			g.dispose();
			// call the lisener 's method
			if (actionListener != null) {
				actionListener.actionPerformed(new ActionEvent(BangBean.this,
						ActionEvent.ACTION_PERFORMED, null));
			}
		}
	}
	
	//////////////////
	class MML extends MouseMotionAdapter{
		public void mouseMoved(MouseEvent e){
			xm=e.getX();
			ym=e.getY();
			repaint();
		}
	}
	public Dimension getPreferredSize(){
		return new Dimension(200,200);
	}

}


package org.rui.swing.bean;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TooManyListenersException;

import javax.swing.JFrame;
import javax.swing.JTextField;

import org.rui.swing.SwingConsole;

public class BangBeanTest extends JFrame {
	private JTextField txt = new JTextField(20);

	// druing testing,report actions:
	class BBL implements ActionListener {
		private int count = 0;
		@Override
		public void actionPerformed(ActionEvent e) {
			txt.setText("BangBean action " + count++);
		}
	}
	//
	public BangBeanTest(){
		BangBean bb=new BangBean();
		try {
			bb.addActionListener(new BBL());
		} catch (TooManyListenersException e) {
			txt.setText("Too many listeners");
		}
		System.out.println("txt:"+txt.getText());
		add(bb);
		add(BorderLayout.SOUTH,txt);
	}
	public static void main(String[] args){
	
		SwingConsole.run(new BangBeanTest(), 400, 500);
	}

}


package org.rui.swing.bean;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.Serializable;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.rui.swing.SwingConsole;
import org.rui.swing.bean.BangBean.ML;
import org.rui.swing.bean.BangBean.MML;

/**
 * java Bean 与同步
 * 
 * @author lenovo
 * 
 */
public class BangBean2 extends JPanel implements Serializable {

	private int xm, ym;
	private int cSize = 20;
	private String text = "Bang2";
	private int fontSize = 48;
	private Color tColor = Color.BLACK;

	private ArrayList<ActionListener> actionListeners = new ArrayList<ActionListener>();

	public BangBean2() {
		addMouseListener(new ML());
		addMouseMotionListener(new MM());
	}

	public synchronized int getCircleSize() {
		return cSize;
	}

	public synchronized void setCircleSize(int cSize) {
		this.cSize = cSize;
	}

	public synchronized String getText() {
		return text;
	}

	public synchronized void setText(String text) {
		this.text = text;
	}

	public synchronized int getFontSize() {
		return fontSize;
	}

	public synchronized void setFontSize(int fontSize) {
		this.fontSize = fontSize;
	}

	public synchronized Color gettColor() {
		return tColor;
	}

	public synchronized void settColor(Color tColor) {
		this.tColor = tColor;
	}

	// /---------------------------------
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(Color.black);
		g.drawOval(xm - cSize / 2, ym - cSize / 2, cSize, cSize);
	}

	public synchronized void addActionListener(ActionListener l) {
		actionListeners.add(l);
	}
	public synchronized void removeActionListener(ActionListener l){
		actionListeners.remove(l);
	}
	public void notifyListener(){
		ActionEvent a=new ActionEvent(BangBean2.this,ActionEvent.ACTION_PERFORMED,null);
		ArrayList<ActionListener> lv=null;
		//make a shallow copy of the list in case
		//someone adds a listener while we're
		//calling listeners
		synchronized(this){
			lv=new ArrayList<ActionListener>(actionListeners);
		}
		//call all the listener methods:
		for(ActionListener al: lv)
			al.actionPerformed(a);
	}
	
	class ML extends MouseAdapter{
		public void mousePressed(MouseEvent e){
			Graphics g=getGraphics();
			g.setColor(tColor);
			g.setFont(new Font("TimesRoman",Font.BOLD,fontSize));
			int width=g.getFontMetrics().stringWidth(text);
			g.drawString(text, (getSize().width-width)/2, getSize().height/2);
			g.dispose();
			notifyListener();
		}
	}
	
	class MM extends MouseMotionAdapter{
		public void mouseMoved(MouseEvent e){
			xm=e.getX();
			ym=e.getY();
			repaint();
		}
	}
	
	public static void main(String[] args) {
		BangBean2 bb2=new BangBean2();
		
		
		bb2.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("ActionEvent action:"+e);
			}
			
		});
		
		
		
		bb2.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("BangBean2 action:");
			}
			
		});
		
		bb2.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("More  action:");
			}
			
		});
		
		JFrame frame=new JFrame();
		frame.add(bb2);
		SwingConsole.run(frame, 300, 300);
		
		
	}
	
	
}



java 编程思想 22.11: java bean 案例代码