Java Swing How to - Change Button Border








Question

We would like to know how to change Button Border.

Answer

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
//from   www . jav  a 2  s . c om
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Fourth Button");
    Container contentPane = frame.getContentPane();
    JButton b = new JButton("Button!");
    Border bored = BorderFactory.createLineBorder(Color.RED);
    b.setBorder(bored);
    contentPane.add(b, BorderLayout.CENTER);
    frame.setSize(350, 200);
    frame.show();
  }
}