有没有办法隐藏标题栏,但将按钮保留在JFrame中 [英] Is there a way to hide the title bar, but keep the buttons in JFrame

查看:109
本文介绍了有没有办法隐藏标题栏,但将按钮保留在JFrame中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在Java Swing中隐藏标题栏,但保留最大化,最小化和关闭按钮.

I was wondering if I could hide the title bar in Java Swing, but keep the maximize, minimize, and close buttons.

我尝试添加frame.setUndecorated(true);,但是它完全删除了最大化,最小化和关闭按钮.

I've tried adding frame.setUndecorated(true); but it removes the maximize, minimize, and close buttons completely.

这是我的代码:

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:否,您将必须使用frame.setUndecorated(true);并自己复制按钮.这样便可以在所有平台上使用,但是要获得本机外观,您必须分别为每个平台实现它.
  • macOS:如果您使用的是jdk 12或更高版本,则可以使用:
  • Windows: No, you will have to use 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:
rootPane.putClientProperty("apple.awt.fullWindowContent", true);
rootPane.putClientProperty("apple.awt.transparentTitleBar", true);

这取自jdk测试用例:

This is taken from the jdk test cases:

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);
});

请注意,用户界面的所有创建和修改都应使用SwingUtilities#invokeLaterSwingUtilities#invokeAndWait在Swing主线程上进行.

Note that all creation and modification of the ui should happen on the Swing main thread using SwingUtilities#invokeLater or SwingUtilities#invokeAndWait.

您要删除标题栏但保留按钮的目的到底是什么?

What exactly is your goal in removing the title bar but keeping the buttons?

这篇关于有没有办法隐藏标题栏,但将按钮保留在JFrame中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆