Java Swing How to - Create JFrame with no caption buttons








Question

We would like to know how to create JFrame with no caption buttons.

Answer

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
//from  w  ww .j  av  a2  s  .c o m
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {
  public static void removeButtons(Component comp) {
    if (comp instanceof AbstractButton) {
      comp.getParent().remove(comp);
    }
    if (comp instanceof Container) {
      Component[] comps = ((Container) comp).getComponents();
      for (int x = 0, y = comps.length; x < y; x++) {
        removeButtons(comps[x]);
      }
    }
  }
  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame();
    frame.setResizable(false);
    removeButtons(frame);
    JPanel panel = new JPanel(new GridBagLayout());
    JButton button = new JButton("Exit");
    panel.add(button, new GridBagConstraints());
    frame.getContentPane().add(panel);
    frame.setSize(400, 300);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    button.addActionListener(e->System.exit(0));
  }
}