【问题标题】:How do you get JFrame from main to make fullscreen?您如何从 main 获取 JFrame 以制作全屏?
【发布时间】:2013-08-05 15:42:42
【问题描述】:

我在 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() 添加参数,顺便说一句。中间有很多复杂而必要的步骤。

【问题讨论】:

    标签: java window jframe fullscreen


    【解决方案1】:

    您的问题有些不清楚,但听起来您希望在窗口模式和全屏模式之间切换 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();        
        }
    }
    

    【讨论】:

    • 感谢您的帮助,但我了解如何使 JFrame 全屏显示。我需要知道如何将 JFrame 从 main 中取出,例如如何在其他类中使用 getter 和 setter。
    • @user2653616:目前还不清楚您要在这里完成什么。如果您想在 main() 之外访问您的 JFrame 对象,则必须将其作为引用存储在其他地方 - 示例包括包含 main() 的类、您的游戏(如上面编辑的示例所示)或您可以从中检索它的其他对象。您不能只在 main() 中实例化它,不传递/存储对它的引用,并期望在其他范围内使用它。
    • 对不起,我措辞错误。我发现我需要在外部声明 JFrame 并在 main() 中使用 game.frame 引用它。感谢您的尝试!
    猜你喜欢
    • 2012-02-16
    • 1970-01-01
    • 2021-03-05
    • 2012-05-12
    • 2021-04-09
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多