Program code: SpeedButton. Java -------------------------------- Import javax. Swing .*; Import java. AWT .*; Import java. AWT. event .*; Public class SpeedButton extends jbutton implements mouselistener { Private Boolean entered = false;/* indicates whether the mouse is on the button */ Private Boolean downed = false;/* indicates whether the button is pressed */ Private Boolean enableddowned = false;/* indicates whether the button is allowed to be pressed */ Private image = NULL; Private int groupid = 0;/* specifies the mutex group to which the button belongs. If the value is 0, no judgment is made */ Private int buttonid;/* ID of the button itself, unique */ /** * This constructor is a bit annoying. Here we can optimize it into multiple constructor combinations. * @ See COM. javax. Swing. jbutton * @ Author delfan, 2002.07.23 * @ Version 1.0 */ Public SpeedButton (INT buttonid, image, int width, int height, string tip, Boolean enableddowned, Boolean initmode, int groupid, actionlistener ){ This. buttonid = buttonid; This. Image = image; Addmouselistener (this ); Setpreferredsize (new dimension (width, height )); Settooltiptext (TIP ); This. enableddowned = enableddowned; This. groupid = groupid; Downed = initmode; Addactionlistener (actionlistener ); } Public void paint (Graphics g ){ If (getsize (). Height = 2 | getsize (). width = 2) {// draw a separator. When the button width or height is 2, G. setcolor (new color (119,133,168); // It is considered as a separator. G. fillrect (0, 0, getwidth ()-1, getheight ()-1 ); G. setcolor (new color (129,143,178 )); G. fillrect (1, 1, getwidth (), getheight ()); Setenabled (false ); } Else { If (entered) {// place the cursor inside the button G. setcolor (new color (119,133,168 )); G. fillrect (0, 0, getwidth (), getheight ()); G. setcolor (new color (8, 36,107 )); G. drawrect (0, 0, getwidth ()-1, getheight ()-1 ); If (image! = NULL) g. drawimage (image, 2, 2, getparent ()); } Else {// The mouse is not in the button G. setcolor (getparent (). getbackground ()); G. fillrect (0, 0, getwidth (), getheight ()); If (image! = NULL) g. drawimage (image, 2, 2, getparent ()); } If (downed) {// The button is pressed G. setcolor (new color (181,190,214 )); G. fillrect (0, 0, getwidth (), getheight ()); G. setcolor (new color (8, 36,107 )); G. drawrect (0, 0, getwidth ()-1, getheight ()-1 ); If (image! = NULL) g. drawimage (image, 3, 3, getparent ()); } } } Public void Update (Graphics g) {paint (g );} /** * Whether the button is in the pressed status */ Public Boolean isdowned () {return downed ;} /** * Obtain the Group Index Number of the button. */ Public int getgroupid () {return groupid ;} Public void setdowned (Boolean downed) {This. downed = downed; repaint ();} Public void mouseentered (mouseevent e) {entered = true; repaint ();} Public void mouseexited (mouseevent e) {entered = false; repaint ();} Public void mouseclicked (mouseevent e ){} Public void mousepressed (mouseevent e ){ If (enableddowned & downed & groupid = 0) Downed = false; Else Downed = true; Repaint (); } Public void mousereleased (mouseevent e ){ If (! Enableddowned) downed = false; Repaint (); If (groupid! = 0) // change the status of the mutex button through the container that stores the button For (INT I = 0; I <getparent (). getcomponentcount (); I ++) If (getparent (). getcomponent (I) instanceof SpeedButton ){ If (SpeedButton) getparent (). getcomponent (I). getgroupid () = groupid & (SpeedButton) getparent (). getcomponent (I ))! = This) (SpeedButton) getparent (). getcomponent (I). setdowned (false ); } } Public int getbuttonid () {return buttonid ;} } |