简体   繁体   English

有没有办法隐藏标题栏,但保留 JFrame 中的按钮

[英]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.如果要保留本机按钮,则取决于操作系统。

  • Windows: No, you will have to use frame.setUndecorated(true); Windows:不,你必须使用frame.setUndecorated(true); and replicate the buttons yourself.并自己复制按钮。 This would then work on all platforms, but to achieve a native look you'd have to implement it for each individually.这将适用于所有平台,但要获得原生外观,您必须为每个平台单独实现它。
  • macOS: If you use jdk 12 or newer you can achieve it using: macOS:如果您使用 jdk 12 或更新版本,您可以使用:
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#invokeLaterSwingUtilities#invokeAndWait在 Swing 主线程上进行。

What exactly is your goal in removing the title bar but keeping the buttons?您删除标题栏但保留按钮的具体目标是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM