如何在应用程序运行时调用 setUndecorated()?

发布于 2024-12-04 13:20:32 字数 1912 浏览 0 评论 0原文

嗨,我在编程方面不是那么专业,所以我来这里是为了问我如何才能做到这一点。

问题:一旦单击全屏模式,客户端游戏就会运行,我希望它调用 setUndecorated(),但在框架已经可见时无法调用。

我意识到我需要创建一个新框架,但我不确定如何将所有内容转移过来,我自己尝试过,我得到的只是新框架的空白屏幕。

这是我的代码:

public Jframe(String args[]) {
    super();
    try {
        sign.signlink.startpriv(InetAddress.getByName(server));
        initUI();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public void initUI() {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
        frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBackground(Color.BLACK);
        gamePanel = new JPanel();
        gamePanel.setLayout(new BorderLayout());
        gamePanel.add(this);
        JMenu fileMenu = new JMenu("Menu");

        String[] mainButtons = new String[] { "-", "Donate", "-", "Vote", "-", "Hiscores", "-", "Forums", "-", "Exit"};

        for (String name : mainButtons) {
            JMenuItem menuItem = new JMenuItem(name);
            if (name.equalsIgnoreCase("-")) {
                fileMenu.addSeparator();
            } else {
                menuItem.addActionListener(this);
                fileMenu.add(menuItem);
            }
        }

        JMenuBar menuBar = new JMenuBar();
        JMenuBar jmenubar = new JMenuBar();
        Jframe.frame.setMinimumSize(new Dimension(773, 556));
        frame.add(jmenubar);
        menuBar.add(fileMenu);
        frame.getContentPane().add(menuBar, BorderLayout.NORTH);
        frame.getContentPane().add(gamePanel, BorderLayout.CENTER);
        frame.pack();

        frame.setVisible(true);
        frame.setResizable(true);
        init();
    } catch (Exception e) {
            e.printStackTrace();
    }

我希望你们中的任何人都可以帮助我,非常感谢!

Hi I'm not such a professional at programming so i come here to ask how i can make this possible.

Issue: Client game is running once fullscreen mode clicked i want it to call setUndecorated() but cant while the frame is already visible.

I realized that i would need to create a new frame but im unsure how to transfer everything over, i have tried myself and all i get is a blank white screen of the new frame.

Here is my code:

public Jframe(String args[]) {
    super();
    try {
        sign.signlink.startpriv(InetAddress.getByName(server));
        initUI();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public void initUI() {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
        frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBackground(Color.BLACK);
        gamePanel = new JPanel();
        gamePanel.setLayout(new BorderLayout());
        gamePanel.add(this);
        JMenu fileMenu = new JMenu("Menu");

        String[] mainButtons = new String[] { "-", "Donate", "-", "Vote", "-", "Hiscores", "-", "Forums", "-", "Exit"};

        for (String name : mainButtons) {
            JMenuItem menuItem = new JMenuItem(name);
            if (name.equalsIgnoreCase("-")) {
                fileMenu.addSeparator();
            } else {
                menuItem.addActionListener(this);
                fileMenu.add(menuItem);
            }
        }

        JMenuBar menuBar = new JMenuBar();
        JMenuBar jmenubar = new JMenuBar();
        Jframe.frame.setMinimumSize(new Dimension(773, 556));
        frame.add(jmenubar);
        menuBar.add(fileMenu);
        frame.getContentPane().add(menuBar, BorderLayout.NORTH);
        frame.getContentPane().add(gamePanel, BorderLayout.CENTER);
        frame.pack();

        frame.setVisible(true);
        frame.setResizable(true);
        init();
    } catch (Exception e) {
            e.printStackTrace();
    }

I hope any of you can help i really appreciate thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡離愁欲言轉身 2024-12-11 13:20:32

如果这是为了游戏,那么您很可能不想要这个。你应该看看
http://download.oracle.com/javase/tutorial/extra/fullscreen /exclusivemode.html

If this is for a game then most likely you don't want this. You should take a look at
http://download.oracle.com/javase/tutorial/extra/fullscreen/exclusivemode.html

秋日私语 2024-12-11 13:20:32

我可能会指出显而易见的事情,并促进错误的方法,但你不能先让它不可见,然后再让它可见吗?

ie

myFrame.setVisible(false);
myFrame.setUndecorated(true);
myFrame.setVisible(true);

但是,正如“ghostbust555”指出的那样,有更好的方法。

这是答案所指的 setFullScreenWindow()

public void goFullScreen() {
    GraphicsDevice myDevice =
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

    // wrap in try because we don't want to lock the app in fullscreen mode
    try {
        myDevice.setFullScreenWindow(myFrame);

        // do your stuff
        ...

    } finally {
        myDevice.setFullScreenWindow(null);
    }
}

I may be pointing out the obvious, as well as facilitating the wrong approach, but can't you just make it not visible then make it visible again?

i.e.

myFrame.setVisible(false);
myFrame.setUndecorated(true);
myFrame.setVisible(true);

However, there is a better approach, as "ghostbust555" pointed out.

This is the setFullScreenWindow() that answer was referring to:

public void goFullScreen() {
    GraphicsDevice myDevice =
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

    // wrap in try because we don't want to lock the app in fullscreen mode
    try {
        myDevice.setFullScreenWindow(myFrame);

        // do your stuff
        ...

    } finally {
        myDevice.setFullScreenWindow(null);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文