Add EmptyBorder to a JButton in Java
Description
The following code shows how to add EmptyBorder to a JButton.
Example
/*from w w w . j ava 2 s . com*/
import java.awt.BorderLayout;
import java.awt.Container;
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("Empty Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border emptyBorder = BorderFactory.createEmptyBorder(20, 20, 0, 0);
JButton emptyButton = new JButton("With Empty");
emptyButton.setBorder(emptyBorder);
JButton nonemptyButton = new JButton("Without Empty");
Container contentPane = frame.getContentPane();
contentPane.add(emptyButton, BorderLayout.NORTH);
contentPane.add(nonemptyButton, BorderLayout.SOUTH);
frame.pack();
frame.setSize(300, frame.getHeight());
frame.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »