Java Swing - 如何創(chuàng)建沒有字幕按鈕的JFrame
我們想知道如何創(chuàng)建沒有字幕按鈕的JFrame。
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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));
}
}