Java programming swing circular button example code
Swing is a GUI toolkit designed for Java.
Swing is part of the Java foundation class.
Swing includes graphical user interface (GUI) devices such as text boxes, buttons, separate panes and tables.
Swing provides many better screen display elements than AWT. They are written in pure Java, so they can run across platforms like Java itself, unlike AWT. They are part of JFC. They support replaceable panels and themes (the default special theme of various operating systems), however, instead of really using the devices provided by the native platform, it only imitates them on the surface. This means that you can use any panel supported by java on any platform. The disadvantage of lightweight components is that they execute slowly, and the advantage is that they can adopt unified behavior on all platforms.
Let's take a look at a simple example of implementing buttons:
result:
Then share a java example of a button that implements a simple click event.
This is a skill about making circular swing buttons. In fact, the knowledge in this technique is convenient for buttons of any shape, but we only make a circular button. When you make a round button, you need to do two things. The first thing is to overload an appropriate drawing method to draw a circle. The second thing is to set some events so that the button will respond only when you click in the range of the circular button (not in the range of the rectangle containing the circular button).
result:
Click event:
Since we want to retain most of the functions of JButton, we let the roundbutton class inherit the JButton class. In the roundbutton constructor, the setcontentareafilled () method is called. This allows the button to draw a rectangular focus area, but not the background.
Now we need to draw a round background. This is achieved by overloading the paintcomponent () method. That method uses graphics The fillOval () method draws a solid circle. Then the paintcomponent () method calls super Paintcomponent () draws a label on top of the solid circle.
This example also overloads the paintborder () method to draw an edge on the boundary of the circular button. If you don't want the border, you can also not overload this method. This method calls graphics The drawOval () method to draw a thin border on the boundary of the circle.
Note: in jdktm1 In 2.2, when you drag the mouse into or out of the range of the button, JButton's behavior has a small bug. In theory, when you click on a circular button and drag the mouse away from the boundary of the button, the button should change its shape. When you drag the mouse into the boundary of the button, the button should revert to its shape. Unfortunately, code that contains this behavior cannot call the contains () method. Instead, use only the 'limit range' of the button (this is the minimum rectangular range containing the button). Note that if you drag the mouse slightly within the circular boundary, that is, leave the circular range but not the boundary, the button will not change its shape.
summary
The above is all about the Java programming implementation of swing circular button example code in this paper. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!