Java Programming Idea 22.11:java Bean case code
Thinking in Java 4 free download: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;/** * Simple bean * @author Lenovo * */public class Frog {private int Ju Mps;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 methodpublic 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;/** * Use Introspector to extract BeanInfo * @author Lenovo * */public class Beandumper extends JFrame {private JTextField query = new Jtext Field (rivate);p jtextarea results = new JTextArea ();p ublic 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;} Gets the Bean property method for (PropertyDescriptor d:bi.getpropertydescriptors ()) {class<?> P = d.getpropertytype (); if (p = = n ULL) 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 {@Overridepublic 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));D umper dmpr = new DuMper (); Query.addactionlistener (DMPR); Query.settext ("Org.rui.swing.bean.Frog");d mpr.actionperformed (new ActionEvent (DMPR, 0, "")); public static void Main (string[] args) {//Tool class Swingconsole.run (new Beandumper (), 600, 500);}} /**outputt:name=========>org.rui.swing.bean.frogproperty Type:java.awt.Colorread Method:+getcolorwrite Method: +setcolor====================================property Type:booleanread Method:+isjmprwrite method:+setJmpr======= =============================property Type:intread Method:+getjumpswrite method:+setjumps======================== ============property Type:org.rui.classts.Petread Method:+getpetwrite 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.ActionListenerlistener Method:actionperformedlistener Methoddescriptor:actionperformedadd 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.KeyListeNerlistener method:keypressedlistener method:keyreleasedlistener method:keytypedlistener methodDescriptor: Keypressedlistener methoddescriptor:keyreleasedlistener Methoddescriptor:keytypedadd 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;/** * a complex 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 () {AddMou Selistener (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);} This is this is a unicast listener//The simplest form of listener management 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 methodif (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;//druing Testing,report Actions:class BBL implements ActionListener {private int count = 0; @Over ridepublic 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 with sync * * @author Lenovo * */public Class BangBean2 extends JPanel implements Serializable {private int xm, ym;private int cSize = 20;private String Text = "B Ang2 ";p rivate int fontSize = 48;private Color tcolor = color.black;private arraylist<actionlistener> Actionlisteners = new arraylist<actionlistener> ();p ublic 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 ' reCalling listenerssynchronized (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 () {@ overridepublic void actionperformed (ActionEvent e) {System.out.println ("ActionEvent action:" +e);}); Bb2.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {System.out.println ( "BangBean2 action:");}); Bb2.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {System.out.println ("more Action:");}}); JFrame frame=new JFrame (); Frame.add (BB2); Swingconsole.run (frame, 300, 300);}}
Java Programming Idea 22.11:java Bean case code