I do this:
package teste;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.JTextField;
public class Teste extends JFrame {
private JButton fechar, mostrar;
private JTextField campo;
private boolean x;
public Teste() {
final Container tela = getContentPane();
tela.setLayout(null);
this.setResizable(false);
setUndecorated(true);
setBackground(new Color(0f, 0f, 0f, 0f));
this.setTitle("Teste");
this.setBounds(0, 0, 400, 400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
this.setLocationRelativeTo(null);
fechar = new JButton();
fechar.setBounds(0, 0, 200, 200);
fechar.setText("Fechar");
fechar.setFont(new Font("Arial", Font.BOLD, 12));
fechar.setBackground(new Color(190, 190, 190));
this.add(fechar);
mostrar = new JButton();
mostrar.setBounds(200, 0, 200, 200);
mostrar.setText("Ocultar");
mostrar.setFont(new Font("Arial", Font.BOLD, 12));
mostrar.setBackground(new Color(190, 190, 190));
this.add(mostrar);
campo = new JTextField("Campo de texto");
campo.setBounds(150, 250, 120, 20);
this.add(campo);
mostrar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mostrar();
}
});
fechar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fechar();
}
});
}
public void fechar() {
System.exit(0);
}
public void mostrar() {
if (!x) {
setBackground(new Color(0f, 0f, 0f, 1f));
mostrar.setText("Ocultar");
x = true;
} else {
setBackground(new Color(0f, 0f, 0f, 0f));//Deixa o frame transparente.
mostrar.setText("Mostrar");
x = false;
}
fechar.setOpaque(x);
repaint();
}
}