让用户按回车键在全屏之间切换

时间:2017-03-08 11:28:25

标签: java swing awt

我有一个简单的框架,有一个按钮可以切换该框架是否全屏。我希望它能让用户只需按回车键即可切换全屏。我是Java的新手。我希望事件监听器位于private FrontEnd()

    import java.awt.event.*;
    import javax.swing.*;

    public class FrontEnd extends JFrame {
        private boolean FullScreenMode = false;
        private int PrevX, PrevY, PrevWidth, PrevHeight;

        public static void main(String[] args) {
            FrontEnd frame = new FrontEnd();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(480, 320);
            frame.setVisible(true);
        }

        private FrontEnd() {

            JPanel contentPane = new JPanel();
            setContentPane(contentPane);

            // From Here starts the trick
            FullScreenEffect effect = new FullScreenEffect();

            JButton fullscreenButton = new JButton("Fullscreen Mode");
            fullscreenButton.addActionListener(effect);

            contentPane.add(fullscreenButton);
            fullscreenButton.setVisible(true);
        }

        private class FullScreenEffect implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (!FullScreenMode) {
                    PrevX = getX();
                    PrevY = getY();
                    PrevWidth = getWidth();
                    PrevHeight = getHeight();

                    dispose();
                    setUndecorated(true);

                    setBounds(0, 0, getToolkit().getScreenSize().width, getToolkit().getScreenSize().height);

                    setVisible(true);
                    FullScreenMode = true;
                } else {
                    dispose();
                    setUndecorated(false);

                    setBounds(PrevX, PrevY, PrevWidth, PrevHeight);

                    setVisible(true);
                    FullScreenMode = false;
                }
            }
        }

2 个答案:

答案 0 :(得分:2)

  

我有一个简单的框架,有一个按钮可以切换该框架是否全屏。我想拥有它,以便用户只需按回车键即可切换全屏。

不要使用KeyListener!

Swing旨在使用Key BindingsThread.sleep()映射到KeyStroke

最简单的方法是将按钮设置为框架的默认按钮,然后按Enter键将激活按钮。

Action

答案 1 :(得分:-2)

管理以使其发挥作用。工作代码如下:

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;

public class FrontEnd extends JFrame implements KeyListener {
    private boolean FullScreenMode = false;
    private int PrevX, PrevY, PrevWidth, PrevHeight;

    public static void main(String[] args) {
        FrontEnd frame = new FrontEnd();
        frame.setTitle("Frame Title");
        frame.setResizable(false);
        frame.setSize(600, 600);
        frame.setMinimumSize(new Dimension(600, 600));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.black);
        frame.pack();
        frame.setVisible(true);
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER)
            FullScreenToggle();
        else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
            ExitApplication();
    }

    public void keyReleased(KeyEvent e) {
    }
    public void keyTyped(KeyEvent e) {
    }

    public FrontEnd(){
        addKeyListener(this);
    }

    public void FullScreenToggle() {
        if (!FullScreenMode) {
            PrevX = getX();
            PrevY = getY();
            PrevWidth = getWidth();
            PrevHeight = getHeight();

            dispose();
            setUndecorated(true);

            setBounds(0, 0, getToolkit().getScreenSize().width, getToolkit().getScreenSize().height);

            setVisible(true);
            FullScreenMode = true;
        } else {
            dispose();
            setUndecorated(false);

            setBounds(PrevX, PrevY, PrevWidth, PrevHeight);

            setVisible(true);
            FullScreenMode = false;
        }
    }

    public void ExitApplication() {
        System.exit(0);
    }
}