Implement quick buttons similar to XP style

Source: Internet
Author: User
Function: similar to the quick button style in Windows XP, the status of a specified button can be automatically displayed (that is, when the mouse key is pressed, the button will be pressed, when the mouse key is raised, the button automatically returns to its original state. It is a switch button (Click once, the button is pressed, click again, and the button is popped up) and can be set to one group (multiple) or multiple sets of mutex buttons (that is, only one button in the same group is always in the pressed State ). of course, this class is not perfect and can be improved in many ways. For example, if a button is not added, it is disabled ), another image should be introduced to show the gray effect. you can change it yourself.

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

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.