Implement a fast button similar to XP style

zhaozj2021-02-16  68

Implementation: Similar to the style of the fast button in Windows XP, allowing the status of the specified button to be bounce (the mouse button is pressed, the button is pressed, the mouse button is lifted when the button is automatically replied to the original) Is a switch button (click once, the button is pressed, then click once, the button is bounted), and can set a set of (multiple) or multiple sets of mutual exclusion buttons (ie, there are only one button in the same group and only one button is always in The state is pressed). Of course, this category is not very perfect, there are many places to improve, such as the status of not adding when the button is forbidden (Disabled), should be introduced into a gray effect. You will I will 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; / * flag for judging whether the mouse over the button * / private boolean downed = False; / * logo, used to press * / private boolean enableddowned = false; / * logo, the button is used to be pressed * / private image image = null; private int groupid = 0; / * designated Which mutual exclusive group belongs to 0 When 0 is 0, it is not judged whether the * / private int buttonid; / * button itself ID number, the only * / / *** This constructor is a bit annoying, here can be optimized into multiple constructors combined. * @see com.javax.swing.JButton * @author delfan, 2002.07.23 * @version 1.0 * / public SpeedButton (int buttonID, Image image, int width, int height, String tip, boolean enabledDowned, boolean initmode , int groupid, ActionListener ActionListener {th is.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 Spacer, when the button width or height is 2 , G.SetColor (New Color (119, 133, 168)); // Thinking is partition g.fillRect (0, 0, getWidth () - 1, GetHeight () - 1); G.SetColor (New Color (NEW Color 129, 143, 178); G.FillRect (1, 1, getWidth (), getHeight ()); setNabled (false);} else {if (entered) {// mouse in the button G.SetColor (New Color (119, 133, 168)); g.fillRect (0, 0, getWidth (), getHeight ());

转载请注明原文地址:https://www.9cbs.com/read-15893.html

New Post(0)