Making a round swing button (Chinese version)

zhaozj2021-02-17  55

This is a trick about making a circular Swing button. In fact, this skill is convenient for any buttons for any shape, but we only make a round button. When you make a round button, you need two things. The first thing is to overload a suitable painting method to draw a circle. The second thing is to set some events to make a response only when you click on the range of the circular button (not the range of the rectangle of the circular button).

Below is a routine that implements a circular button:

Import java.awt. *; import java.awt.geom. *; import javax.swing. *;

Public Class Roundbutton Extends JButton {Public Roundbutton (String Label) {Super (Label);

// These statements extend the button into a circle instead of an ellipse. Dimension size = getpreferredsize (); size.width = size.Height = math.max (size.width, size.height); setPreferredSize (size);

// This call makes JButton do not draw background, and let us draw a round background. SetContentareAfilled (false);

// Painted rounds and labels Protected Void PaintComponent (GRAPHICS G) {if (getModel (). Isarmed ()) {

/ / You can choose a highlighted color as a property g.setcolor (color.lightgray);} else {g.setcolor (GetBackground ());} g.filloval (0, 0, getSize () .width-1, getsize (). HEIGHT-1);

// This call will draw a label and focus rectangle. Super.PaintComponent (g);

// With a simple arc painting button boundary. Protected Void PaintBorder (G. G.SetColor (getForeground ()); g.drawoval (0, 0, getSize (). Width-1, getSize (). Height-1);}

// Detect Click on the event Shape Shape; Public Boolean Contains (INT X, INT Y) {

// If the button changes the size, generate a new shape object. IF (shape == null ||! shape.getbounds (). Equals (getBounds ())) {shape = new elipse2d.float (0, 0, getWidth (), getHeight ());} return shape.contains (x Y);

// Test program public static void main (String [] args) {// generates a button with a 'JackPot' tag. JButton Button = New Roundbutton ("jackpot"); Button.SetBackground (Color.green);

// Generate a frame to display this button. JFrame frame = new JFrame ();. Frame.getContentPane () setBackground (Color.yellow); frame.getContentPane () add (button);. Frame.getContentPane () setLayout (new FlowLayout ());. Frame.setSize ( 150, 150); Frame.setVisible (TRUE);}} We let the Roundbutton class inherit the JButton class because we want to keep JButton's most features. In the constructor of RoundButton, the setContentAreafilled () method is called. This allows the button to paint a rectangular focus area, but do not draw background.

Now we need to draw a round background. This is achieved by overloading the PaintComponent () method. That ways draw a solid circle using Graphics.FillOval (). Then the PaintComponent () method calls super.paintcomponent () draws a label on this solid round.

This example also overloads the PaintBorder () method to draw a side on the boundary of the circular button. If you don't want a border, you may not overload this method. This method calls the Graphics.DrawovAl () method to draw a fine border on the circular boundary.

Note: In JDKTM 1.2.2, when you drag the mouse to or drag the button, the JButton behavior has a small bug. In theory, when you click on the rotation of the mouse on the circular button and drag the mouse to leave the button, the button should change its shape. When you drag the mouse to enter the boundary of the button, the button should reply to its shape. Unfortunately, the code containing this behavior cannot call the Contains () method. Instead, it is noted that only the 'limit range of the button (this is the smallest rectangular range containing the button), if you slightly drag the mouse in the circular boundary, that is, left the circle range but not leave the boundary. The button will not change its shape.

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

New Post(0)