[英]Is there a way to hide the title bar, but keep the buttons in JFrame
I was wondering if I could hide the title bar in Java Swing, but keep the maximize, minimize, and close buttons.我想知道是否可以隐藏 Java Swing 中的标题栏,但保留最大化、最小化和关闭按钮。
I've tried adding frame.setUndecorated(true);
我试过添加
frame.setUndecorated(true);
but it removes the maximize, minimize, and close buttons completely.但它完全删除了最大化、最小化和关闭按钮。
Here is my code:这是我的代码:
public Display(String title, int width, int height) {
Display.width = width;
Display.height = height;
Display.absWidth = width;
Display.absHeight = height;
Display.d = new Dimension(width, height);
setProperties();
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(width, height));
frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(canvas, BorderLayout.CENTER);
frame.setIgnoreRepaint(true);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setResizable(true);
canvas.createBufferStrategy(2);
bs = canvas.getBufferStrategy();
g = bs.getDrawGraphics();
frame.getRootPane().putClientProperty("apple.awt.fullWindowContent", true);
frame.getRootPane().putClientProperty("apple.awt.transparentTitleBar", true);
frame.setVisible(true);
handleResize();
handleQuit();
//showSplashScreen();
}
If you want to keep the native buttons then it depends on the operating system.如果要保留本机按钮,则取决于操作系统。
frame.setUndecorated(true);
frame.setUndecorated(true);
and replicate the buttons yourself.rootPane.putClientProperty(“apple.awt.fullWindowContent“, true);
rootPane.putClientProperty(“apple.awt.transparentTitleBar“, true);
This is taken from the jdk test cases:这取自 jdk 测试用例:
SwingUtilities.invokeLater(() -> {
frame = new JFrame("Test");
frame.setBounds(200, 200, 300, 100);
rootPane = frame.getRootPane();
JComponent contentPane = (JComponent) frame.getContentPane();
contentPane.setBackground(Color.RED);
rootPane.putClientProperty("apple.awt.fullWindowContent", true);
rootPane.putClientProperty("apple.awt.transparentTitleBar", true);
frame.setVisible(true);
});
Note that all creation and modification of the ui should happen on the Swing main thread using SwingUtilities#invokeLater
or SwingUtilities#invokeAndWait
.请注意,UI 的所有创建和修改都应使用
SwingUtilities#invokeLater
或SwingUtilities#invokeAndWait
在 Swing 主线程上进行。
What exactly is your goal in removing the title bar but keeping the buttons?您删除标题栏但保留按钮的具体目标是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.