How to Create and Design Rounded Button In Java Netbeans
In this Java Tutorial we will see How To Create two rounded Jbuttons, and clicking each button will trigger a message dialog.
The rounded buttons have a gradient-colored background
What We Are Gonna Use In This Project:
- Java Programming Language.- NetBeans Editor.
Project Source Code:
package new_tutorials;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.plaf.basic.BasicButtonUI;
/**
*
* @author 1BestCsharp
*/
public class RoundedButtonFrame extends JFrame{
public RoundedButtonFrame(){
setTitle("Rounded Button Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,150);
setLocationRelativeTo(null);
initializeUI();
}
private void initializeUI(){
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER,20,40));
RoundedButton button1 = new RoundedButton("Button 1");
button1.setBackground(new Color(255,69,96));
RoundedButton button2 = new RoundedButton("Button 2");
button2.setBackground(new Color(70,130,180));
button1.addActionListener((e) -> {
JOptionPane.showMessageDialog(this, "Button 1 Clicked");
});
button2.addActionListener((e) -> {
JOptionPane.showMessageDialog(this, "Button 2 Clicked");
});
panel.add(button1);
panel.add(button2);
add(panel);
}
public static void main(String[] args) {
RoundedButtonFrame frame = new RoundedButtonFrame();
frame.setVisible(true);
}
}
// Create a custom JButton class for rounded buttons
class RoundedButton extends JButton{
public RoundedButton(String text){
super(text);
setUI(new RoundedButtonUI());
setFont(new Font("Arial",Font.BOLD, 16));
setForeground(Color.WHITE);
setCursor(new Cursor(Cursor.HAND_CURSOR));
}
}
// Create a custom UI class for rendering rounded buttons
class RoundedButtonUI extends BasicButtonUI{
@Override
public void installUI(JComponent c){
super.installUI(c);
AbstractButton button = (AbstractButton) c;
button.setOpaque(false);
button.setBorderPainted(false);
}
@Override
protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text){
super.paintText(g, c, textRect, text);
}
@Override
public void paint(Graphics g, JComponent c){
AbstractButton btn = (AbstractButton) c;
// Paint the background with rounded corners
paintBackground(g, btn, btn.getModel().isPressed() ? 2 : 0);
super.paint(g, c);
}
// Method to paint the background with rounded corners
private void paintBackground(Graphics g, JComponent c, int yOffset){
Dimension size = c.getSize();
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Fill a rounded rectangle with a darker color
GradientPaint gradientPaint = new GradientPaint(0, yOffset, c.getBackground().brighter(), 0,size.height - yOffset,c.getBackground().darker());
g2d.setPaint(gradientPaint);
g2d.fillRoundRect(0, yOffset, size.width, size.height - yOffset, 25, 25);
g2d.dispose();
}
}
The Final Result:
More Java Projects:
Download Projects Source Code