JButton with round edges

2

My question at this time is whether there is any method or class predefined in java to be able to create a button with round edges.

    
asked by José A. Solís 22.04.2017 в 03:24
source

1 answer

3

The JButton actually has round edges, what you can do is increase the radius of the angulo,

as a proposal this class:

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.border.Border;

class RoundedBorder implements Border {

    private int radius;

    RoundedBorder(int radius) {
        this.radius = radius;
    }

    public Insets getBorderInsets(Component c) {
        return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);
    }

    public boolean isBorderOpaque() {
        return true;
    }

    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        g.drawRoundRect(x, y, width-1, height-1, radius, radius);
    }
}

You create your button and define the class with the desired radius in the Border property:

//Crea boton.
 JButton myButton = new JButton("my button");
 //Define 40 como Radio.
 myButton.setBorder(new RoundedBorder(40)); 
    
answered by 22.04.2017 / 06:28
source