仅在JFrame上显示未修饰的关闭按钮 [英] showing only close button on the JFrame undecorated

查看:103
本文介绍了仅在JFrame上显示未修饰的关闭按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以取消装饰JFrame然后只显示顶部的关闭按钮或类似的东西..任何帮助将不胜感激。
详细信息:
实际上我正在制作一个项目,这是一个基于GUI的软件,我使用了JdesktopPane,我使用的是JInternalFrames,所以显示顶部的标题栏不仅适合我。有没有办法解决我的问题。

I want to know if it is possible to undecorate a JFrame and then show only close button at the top or something similar to that.. Any help will be greatly appreciated. Detail: Actually i am making a project, which is a GUI based software, i have used JdesktopPane in it and i am using JInternalFrames so showing the titlebar on the top is not just suiting me. Is there any way to solve my problem.

推荐答案

基本上,一旦你删除框架边框,你就要对它的功能负责,包括移动。

Basically, once you remove the frame border, you become responsible for it's functionality, including moving.

此示例基本上使用 JPanel 作为标题窗格的占位符,使用 BorderLayout 来布局标题和内容。

This example basically uses a JPanel to act as a place holder for the "title" pane and uses a BorderLayout to layout out the title and content.

实际关闭按钮是一个简单的 JButton 使用一些图像来表示关闭动作。显然,这只会在Windows上看起来正常,这些按钮是由操作系统本身生成的......我们无法访问该层......

The actual close button is a simple JButton which is using some images to represent the close action. Obviously, this is only going to look right on Windows, these buttons are normal generated by the OS itself...and we don't have access to that layer...

public class TestUndecoratedFrame {

    public static void main(String[] args) {
        new TestUndecoratedFrame();
    }

    public TestUndecoratedFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                TitlePane titlePane = new TitlePane();

                JFrame frame = new JFrame("Testing");
                frame.setUndecorated(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                ((JComponent)frame.getContentPane()).setBorder(new LineBorder(Color.BLACK));
                frame.add(titlePane, BorderLayout.NORTH);
                frame.add(new JLabel("This is your content", JLabel.CENTER));
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TitlePane extends JPanel {

        public TitlePane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1;
            gbc.anchor = GridBagConstraints.EAST;
            add(new CloseControl(), gbc);
        }
    }

    public class CloseControl extends JButton {

        public CloseControl() {
            setBorderPainted(false);
            setContentAreaFilled(false);
            setFocusPainted(false);
            setOpaque(false);
            setBorder(new EmptyBorder(0, 0, 8, 12));
            try {
                setRolloverIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Highlighted.png"))));
                setRolloverEnabled(true);
                setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Normal.png"))));
            } catch (IOException ex) {
                Logger.getLogger(TestUndecoratedFrame.class.getName()).log(Level.SEVERE, null, ex);
            }

            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    SwingUtilities.getWindowAncestor(CloseControl.this).dispose();
                }
            });
        }
    }
}

这篇关于仅在JFrame上显示未修饰的关闭按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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