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);

	}

}
时间: 2024-07-30 08:37:37

java 编程思想 22.11: java bean 案例代码的相关文章

39.JAVA编程思想之外篇——JAVA图形化设计精简大全一文覆盖

39.JAVA编程思想之外篇--JAVA图形化设计精简大全一文覆盖 欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron/article/details/51204948 目录 Java图形化界面设计--容器(JFrame)...1 Java基本类(JFC)...1 l     AWTAbstract Window Toolkit(AWT)抽象窗口工具包... 2 l     Swing包... 2 l     AWT和Swing的区别... 6 Swing基本框

Myeclipse 导入《JAVA编程思想(Think in Java 4th)》中的ant项目

版权声明:仅允许在正文前注明作者和链接的情况下,进行非商业性转载,转载时本文正文不可减少内容.修改内容或者增加内容(包括图片) <JAVA编程思想(Think in Java)>提供了大量源代码,可是项目是用ant构建的.对于用惯了eclipse,netbeans等IDE的同学们可能有些手足无措,这里分享一些本人在阅读这本书时使用ant中的一些心得.如果疏漏之处还请指教,欢迎大家和我交流~ 1.下载源代码 书中的源代码,还有在命令行界面下使用ANT的配置详细说明(均来自书籍原作者),我都一并打

关于阅读java编程思想和effective java的一些看法

个人认为,java编程思想并不适合当作新手入门书籍来看,它更多是像给已经使用过java的人群对于基础的一些查缺补漏,有点像一本大部头的工具书,目前该书已看至第十章 -- 内部类, 而effective java这本书,更多是如所说的,是一本分享经验与指引你避免走弯路的经典著作,针对如何编写高效.设计优良的程序提出了最实用.最权威的指导方针,目前该书只看至第三章.我本来是想看完一个章节来写一个读书笔记,但目前来看,这样不合适,都是经典书籍,好书就该多读几次,所以第一遍初读只是大概了解书籍所讲的内容

Java编程思想学习(十三) java I/O

Java中使用流来处理程序的输入和输出操作,流是一个抽象的概念,封装了程序数据于输入输出设备交换的底层细节.JavaIO中又将流分为字节流和字符流,字节流主要用于处理诸如图像,音频视频等二进制格式数据,而字符流主要用于处理文本字符等类型的输入输出. 1.字节输入流InputStream 输入流InputStream负责从各种数据/文件源产生输入,输入源包括:数组,字符串,文件,管道,一系列其他类型的流,以及网络连接产生的流等等. 常用字节输入流的主要类型: (1).ByteArrayInputS

【Java编程思想】11.持有对象

如果一个程序只包含固定数量的且生命周期都是已知的对象,那么这是一个非常简单的程序. Java 类库中提供一套容器类,来存储比较复杂的一组对象.其中有 List.Set.Queue.Map 等.这些类也被称为集合类,Java 的类库中使用 Collection 这个名字指代该类库的一个特殊子集(其实 Java 中大部分容器类都实现了 Collection 接口). 11.1 泛型和类型安全的容器 在 Java SE5 之前的容器,编译器是允许向容器中插入不正确的类型的.因此在获取容器中对象时,一旦

java编程思想第11章练习16

创建一个元音字母Set.对UniqueWords.java操作,计数并显示在每一个输入单词中的元音字母数量,并显示输入文件中的所有元音字母的数量总和. public class Vowels6 { static void vowelCounter(Set<String> st) { Set<Character> vowels = new TreeSet<Character>(); Collections.addAll(vowels, 'A', 'E', 'I', 'O'

Java编程思想 - 第11章 持有对象

· 容器类: 1. Collection: List, Set, Queue 2. Map · List<Apple> apples = new ArrayList<Apple>(); 这种方式并非总能奏效,因为某些类具有额外的功能,例如,LinkList具有在List接口中未包含的额外方法,而TreeMap也具有在Map接口中未包含的方法.如果你需要使用这些方法,就不能将它们向上转型为更通用的接口. · Collection.addAll()成员方法只能接受另一个Collecti

【java编程思想--学习笔记(四)】对象导论

写这篇博客的前言: 长话短说,我希望通过阅读<java编程思想>来使我的代码 简洁可用 . 目的的层次不同,首先具体的目标是,了解Java的特性和巩固Java的基础. 更抽象的目的如下: 1.期待以巩固基础的方式,使代码优美,简洁,高效. 2.使自己写的模块能够开放适度,好用. 3.形成一种对代码是否优美的审美观. 于是<Java编程思想>第一章 对象导论 由此开始. 1.1 抽象过程 java 相对于命令式语言的优势在于只针对于待解问题建模.后者所做的主要抽象要求所做问题基于计算

《Java编程思想》第十三章 字符串

<Java编程思想>读书笔记 1.String作为方法的参数时,会复制一份引用,而该引用所指的对象其实一直待在单一的物理位置,从未动过. 2.显式地创建StringBuilder允许预先为他指定大小.如果知道字符串多长,可以预先指定StringBuilder的大小避免多次重新分配的冲突. 1 /** 2 * @author zlz099: 3 * @version 创建时间:2017年9月1日 下午4:03:59 4 */ 5 public class UsingStringBuilder {