Swing is a GUI toolkit designed for Java.
Swing is part of the JAVA basic class.
Swing includes graphical user interface (GUI) devices such as text boxes, buttons, separating panes and tables.
Swing provides many better screen display elements than AWT. They are written in pure Java, so they can run across platforms just like Java itself, which is not like AWT. They are part of JFC. They support replaceable panels and themes (specific themes for various operating systems defaults), but instead of really using devices provided by native platforms, they simply mimic them on the surface. This means you can use any panel supported by JAVA on any platform. The disadvantage of lightweight components is that they are slow to execute, and the advantage is that they can adopt unified behavior on all platforms.
Let's take a look at a simple example of implementing the button:
import java.awt.*;import java.awt.geom.*;import javax.swing.*;class RButton extends JButton {public RButton(String label) {super(label);// These declarations extend the button to 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 not draw the background, but allows us to draw a circle background. setContentAreaFilled(false); this.setBackground(Color.GRAY);}// Draw the background and label of the circle protected void paintComponent(Graphics g) {if (getModel().isArmed()) {// You can choose a highlighted color as the property of the circular button class 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);}// Use a simple arc to draw the boundary of the button. protected void paintBorder(Graphics g) {g.setColor(getForeground());g.drawOval(0, 0, getSize().width - 1, getSize().height - 1);}// Detect click event Shape shape;public boolean contains(int x, int y) {// If the button changes size, a new shape object is generated. if (shape == null || !shape.getBounds().equals(getBounds())) {shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());} return shape.contains(x, y);}// Test program public static void main(String[] args) {// Generate a button with the 'Jackpot' tag. JButton button = new RButton("Jackpot");ImageIcon ic = new ImageIcon("E://clientForMssql//Icons//item_group.gif");JButton button2 = new JButton(ic);button.setBackground(Color.GRAY);// Generate a frame to display this button. JFrame frame = new JFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// frame.getContentPane().setBackground(Color.GRAY);frame.getContentPane().add(button);frame.getContentPane().add(button2);frame.getContentPane().setLayout(new FlowLayout());frame.setSize(200, 200);frame.setVisible(true);}}result:
Next, I will share a Java example of buttons that implement simple click events.
Here is a tip for making round Swing buttons. In fact, this trick is conveniently applicable to buttons of any shape, but we only make a round 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 on the range of the circular button (not the range of the rectangle containing the circular button).
import java.awt.*;import java.awt.geom.*;import javax.swing.*;public class RoundButton extends JButton {public RoundButton(String label) {super(label);// These declarations extend the button to 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 not draw the background, but allows us to draw a circle background. setContentAreaFilled(false);}// Draw the background and label of the circle protected void paintComponent(Graphics g) {if (getModel().isArmed()) {// You can select a highlighted color as the property of the circular button class 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);}// Use a simple arc to draw the boundary of the button. protected void paintBorder(Graphics g) {g.setColor(getForeground());g.drawOval(0, 0, getSize().width - 1, getSize().height - 1);}// Detect click event Shape shape;public boolean contains(int x, int y) {// If the button changes size, a new shape object is generated. if (shape == null || !shape.getBounds().equals(getBounds())) {shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());} return shape.contains(x, y);}// Test program public static void main(String[] args) {// Generate a button with the '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);}}result:
Click Event:
Since we want to preserve most of the functionality of JButton, we let the RoundButton class inherit the JButton class. In the RoundButton constructor, the setContentAreaFilled() method is called. This makes the button draw a rectangular focus area, but not the background.
Now we need to draw a circle background. This is achieved by overloading the paintComponent() method. That method uses the Graphics.fillOval() method to draw a solid circle. Then the paintComponent() method calls super.paintComponent() to draw a label on the solid circle.
This example also overrides the paintBorder() method to draw an edge on the boundary of the circular button. If you don't want borders, you can also not overload this method. This method calls the Graphics.drawOval() method to draw a thin border on the boundary of the circle.
Note: In JDKTM1.2.2, when you drag the mouse into or out of the button's range, JButton's behavior has a small bug. Theoretically, when you click the mouse on the circular button and then drag the mouse away from the button's boundary, the button should change its shape. When you drag the mouse into the button's boundary, the button should reply to its shape. Unfortunately, the code containing this behavior cannot call the contains() method. Instead it is to use only the 'limit range' of the button (which is the minimum rectangular range that contains the button). Note that if you drag the mouse slightly within the boundary of the circle, that is, leave the range of the circle but not the boundary, the button will not change its shape.
Summarize
The above is all the content of this article about Java programming to implement swing round button instance code, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!