0

我在 main 中定义了我的 JFrame,我想这样做,所以如果我点击一个按钮,按 F 等,屏幕就会全屏显示。我了解如何执行此操作,但我不明白如何将 JFrame 实例从 setFullScreenWindow 的 main 中取出,就像您如何使用 getter 获取玩家的 x 一样。

这是我的代码示例:

private boolean fullscreen = false;

public static void main(String args[]){
    Game game = new Game();

    //Set size of game (not shown)

    JFrame frame = new JFrame(game.TITLE);
    frame.add(game);
    //JFrame setup stuff (not shown)

    game.start();
}

public void setFullScreen(JFrame frame){
    if(isFullScreenSupported){
        if(!fullscreen){
            frame.setUndecorate(true);
            gd.setFullScreenWindow(frame);
            frame.validate;
        } else{
            gd.setFullScreenWindow(null);
        }
     }
}

顺便说一句,我无法向 start() 添加参数。中间有很多复杂而必要的步骤。

4

1 回答 1

0

您的问题有些不清楚,但听起来您希望在窗口模式和全屏模式之间切换 JFrame。这是一个如何做到这一点的独立示例:

package example.stackoverflow;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class FullScreenExample
{    
    static class GameFrame extends JFrame
    {
        private static final long serialVersionUID = 5386744421065461862L;
        private static final GraphicsDevice gd = (GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices())[0];
        private static final boolean FULLSCREEN_SUPPORTED = gd.isFullScreenSupported();
        private static final String MAKE_FULLSCREEN_TEXT = "Make Full Screen";
        private static final String MAKE_WINDOWED_TEXT = "Make Windowed";
        private static final int WINDOWED_WIDTH = 400;
        private static final int WINDOWED_HEIGHT = 300;

        private final JButton fsButton = new JButton(MAKE_FULLSCREEN_TEXT);
        private boolean isFullscreen = false;

        public GameFrame(String title)
        {
            super(title);
            setSize(WINDOWED_WIDTH, WINDOWED_HEIGHT);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            initComponents();
        }

        public void initComponents()
        {
            setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
            if(FULLSCREEN_SUPPORTED)
            {
                fsButton.addActionListener(new ActionListener()
                {       
                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        toggleFullscreen();
                    }
                });
                add(fsButton);
            }
            else
            {
                add(new JLabel("Fullscreen mode is not supported on this device"));
            }
        }

        public void toggleFullscreen()
        {
            isFullscreen = !isFullscreen;
            setVisible(false);
            dispose();
            setUndecorated(isFullscreen);
            if(isFullscreen)
            {
                fsButton.setText(MAKE_WINDOWED_TEXT);
                gd.setFullScreenWindow(this);
                validate();
            }
            else
            {
                fsButton.setText(MAKE_FULLSCREEN_TEXT);
                gd.setFullScreenWindow(null);
                setVisible(true);
            }
        }
    }

    static class Game
    {
        private GameFrame gameFrame;

        public Game(String title)
        {
            gameFrame = new GameFrame(title);
        }

        public void start()
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    getGameFrame().setVisible(true);
                }
            });
        }

        public GameFrame getGameFrame()
        {
            return gameFrame;
        }
    }

    public static void main(String[] args)
    {
        Game g = new Game("Foo");
        g.start();        
    }
}
于 2013-08-05T18:26:01.380 回答