使单个组件全屏显示 [英] Making a single component full screen

查看:200
本文介绍了使单个组件全屏显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你点击按钮时,我正试图让JPanel全屏显示,当你按下逃生时我再次返回。

I'm trying to make a JPanel go full screen when you click a button, and back again when you press escape.

我设法得到了窗口全屏,但由于添加组件从其他容器中删除它的全部内容,我最终得到一个空白的JPanel。

I've managed to get the window to go full screen, but because of the whole thing about adding components removing them from other containers, I end up with a blank JPanel.

我选择单独制作JFrame渲染全屏,其类如下(请注意,这是一个内部类,因此myPanel引用已存在于MyJFrame中的面板):

I chose to make a separate JFrame to render full screen, the class of which is as follows (note that this is an inner class, so myPanel refers to a panel that already exists in MyJFrame):

public class FullScreen extends JFrame {

    private static final long serialVersionUID = 1L;

    private GraphicsDevice device;

    private boolean isFullScreen;

    public FullScreen() {
        this.setContentPane(myPanel);
        this.setUndecorated(true);

        // Fullscreen return
        this.addKeyListener(new KeyListener() {
            @Override
            public void keyPressed(KeyEvent e) {
                // Exit fullscreen when ESC pressed
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    exitFullScreen();
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

            @Override
            public void keyTyped(KeyEvent e) {
            }
        });
    }

    public void enterFullScreen() {
        if (!isFullScreen) {
            // Get the current device
            GraphicsEnvironment graphicsEnvironment = 
                    GraphicsEnvironment.getLocalGraphicsEnvironment();
            device = graphicsEnvironment.getDefaultScreenDevice();

            if (device.isFullScreenSupported()) {
                // Make the current window invisible
                MyJFrame.this.setVisible(false);
                // Set the full screen window
                device.setFullScreenWindow(this);
                isFullScreen = true;
            }
        }
    }

    public void exitFullScreen() {
        if (isFullScreen) {
            // Reset the full screen window
            device.setFullScreenWindow(null);
            MyJFrame.this.setVisible(true);
            isFullScreen = false;
        }
    }
}

关于如何看待任何其他明智的想法要完成这个吗?

Any other bright ideas on how to accomplish this?

推荐答案

这是我的类内置于一个非常好用的示例中。我确定我没有正确处理和验证框架,所以请对它进行评论,以便我可以更新它。

Here's my class built into an example that works very nicely. I'm sure I'm not disposing and validating the frame properly so please comment on it so I can update it.

public class FullScreenExample extends JFrame {

    public class FullScreen {
        private GraphicsDevice device;
        private JFrame frame;
        private boolean isFullScreen;

        public FullScreen() {
            frame = new JFrame();
            JPanel content = new JPanel();
            content.setLayout(new BorderLayout());
            frame.setContentPane(content);
            frame.setUndecorated(true);

            // Full screen escape
            frame.addKeyListener(new KeyListener() {
                @Override
                public void keyPressed(KeyEvent e) {
                    // Exit full screen when ESC pressed
                    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                        exitFullScreen();
                    }
                }

                @Override
                public void keyReleased(KeyEvent e) {}

                @Override
                public void keyTyped(KeyEvent e) {}
            });
        }

        public void enterFullScreen() {
            if (!isFullScreen) {
                // Get the current device
                GraphicsConfiguration config = FullScreenExample.this.getGraphicsConfiguration();
                device = config.getDevice();

                // Remove the panel from the wrapper
                myWrapper.remove(myPanel);
                // Add the panel to the full screen frame
                frame.getContentPane().add(myPanel);
                // Set the full screen window
                device.setFullScreenWindow(frame);
                isFullScreen = true;
            }
        }

        public void exitFullScreen() {
            if (isFullScreen) {
                // Remove the fractal from the full screen frame
                frame.getContentPane().remove(myPanel);
                // Add the panel back to the wrapper
                myWrapper.add(myPanel);
                // Disable full screen
                device.setFullScreenWindow(null);
                // Dispose frame
                frame.dispose();
                // Revalidate window
                FullScreenExample.this.validate();
                isFullScreen = false;
            }
        }
    }

    /*
     * This example uses a main content panel, myPanel
     * and a wrapper to host the panel in the main JFrame, myWrapper.
     */
    private JPanel myPanel, myWrapper;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                FullScreenExample frame = new FullScreenExample();
                frame.init();
                frame.setVisible(true);
            }
        });
    }

    public void init() {
        // Generate example main window
        JPanel content = new JPanel();
        content.setBorder(new EmptyBorder(5, 5, 5, 5));
        content.setLayout(new BorderLayout());
        this.setContentPane(content);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        myPanel = new JPanel();
        myPanel.setBackground(Color.BLUE);

        // Full screen button and listener
        JButton fullscreen = new JButton("Full Screen");
        final FullScreen fs = new FullScreen();
        fullscreen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                fs.enterFullScreen();
            }
        });

        myWrapper = new JPanel();
        myWrapper.setLayout(new BorderLayout());
        myWrapper.add(myPanel);

        content.add(myWrapper, BorderLayout.CENTER);
        content.add(fullscreen, BorderLayout.SOUTH);
        this.setBounds(100, 100, 350, 350);
    }
}

这篇关于使单个组件全屏显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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