Set Margin for JButton in Java

Description

The following code shows how to set Margin for JButton.

Example


import java.awt.Insets;
//from  w  ww .j  a  va2  s  . c o m
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main extends JFrame {

  public Main() {
    setSize(300, 150);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JButton button = new JButton("java2s.com");
    button.setMargin(new Insets(20, 20, 20, 20));
    add(button);
    setVisible(true);
  }

  public static void main(String[] args) {
    new Main();
  }
}

The code above generates the following result.

Set Margin for JButton in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer