当我将程序设置为全屏时,为什么JPanel无法正确呈现? [英] Why does the JPanel not render correctly when i set my program to fullscreen?

查看:105
本文介绍了当我将程序设置为全屏时,为什么JPanel无法正确呈现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个将我的JFrame设置为全屏的JButton:

I made a JButton that sets my JFrame to fullscreen:

    add(fullscreen);

    fullscreen.setSize(100, settings.getHeight());
    fullscreen.setLocation(settings.getX() + (settings.getWidth() / 2) - 50, light.getY() + 35);

    fullscreen.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {

        if(isFullscreen) {

            isFullscreen = false;

            screen.ifNotFullscreen();
        }


        if(!isFullscreen) {

            isFullscreen = true;

            screen.ifFullscreen(screen);
        }

        mm.revalidate();
    }
});

这里是JFrame:

public class Screen extends JFrame {

    private Kingdomcraft kd;
    private MainMenu mm;
    private Screen screen;
    private Dimension min = new Dimension(800, 600);
    private ImageIcon img;

    public void run() {

        kd = new Kingdomcraft();
        mm = new MainMenu();
        screen = new Screen();
        img = new ImageIcon("assets/textures/items/ItemSwordIron.png");

        this.add(kd);
        this.setContentPane(mm);
        this.setLocationRelativeTo(null);
        this.setTitle("Kingdomcraft");
        this.setIconImage(img.getImage());
        this.setBackground(Color.BLACK);
        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mm.setOpaque(false);
        mm.run();

        if (mm.isFullscreen) {

            ifFullscreen(screen);
        }

        if (!mm.isFullscreen) {

            ifNotFullscreen();
        }
    }

    public void ifFullscreen(Screen screen) {

        this.screen = screen;
        this.setVisible(true);

        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(screen);

        repaint();
    }

    public void ifNotFullscreen() {

        this.setMinimumSize(min);
        this.setResizable(true);
        this.setVisible(true);

        repaint();
    }
}

为什么当我运行游戏并选择时JBaton全屏显示错误的JPanel吗?

why is it that when i run the game and select the JButton fullscreen the JPanel renders incorrectly?

我的意思是在我的黑色背景上有一个没有JButton的大灰色JPanel。

And by this I mean there is a big grey JPanel with no JButtons rendered on top of my black background.

推荐答案

问题是你正在创建多个不同的 Screen 实例,但你绝对有不知道哪一个在屏幕上实际可见。

The problem is you are creating multiple, distinct, instances of Screen but you have absolutely no idea which one is actually visible on the screen.

Kingdomcraft #main ,创建一个<$ c的实例$ c>屏幕并调用 screen.run(),创建一个 Kindomcraft , MainMenu 屏幕 ...但等等,我们屏幕,为什么我们需要另一个实例?然后你继续向这个屏幕实例添加内容,留下屏幕的实例空白...

Kingdomcraft#main, creates an instance of Screen and calls screen.run(), which creates an instance of Kindomcraft, MainMenu and Screen...but wait, we are Screen, why do we need another instance? You then go on to add content to this instance of Screen, leaving the instance of screen blank...

什么是奇怪的,你会传递屏幕的实例 ifFullscreen (这是空白版本),然后尝试将其全屏显示...没有任何内容......但是 ifNotFullscreen 实际上使用这个 ???

What's really weird, you would pass the instance of screen to ifFullscreen (this is the blank version) which would then try and make it full screen...with nothing on it...but ifNotFullscreen actually uses this???

什么甚至更奇怪...当你打电话 MainMenu#run 屏幕#run ,你创建一个 Kingdomcraft MainMenu 屏幕 ... AGAIN ...

What's even weirder...when you call MainMenu#run from Screen#run, you create a new instance of Kingdomcraft, MainMenu and Screen...AGAIN...

全屏按钮被执行时,你传递的是你在<创建的屏幕的引用code> MainMenu ,这不是包含内容并且应该在屏幕上显示的那个...

When the fullscreen button is actioned, you are passing it the reference of screen you created in MainMenu, which is NOT the one that has content on it and which should be displayed on the screen...

基本上,你似乎不明白 new 的作用和di对象实例之间的差异。新实例与同一对象的任何其他实例没有任何关系...

Basically, you don't seem to understand what new does and the differences between instance of objects. A new instance has no relationship to any other instances of the same object...

您应该做的是传递 Kingdomcraft <的引用/ code>到屏幕然后将 Screen 的引用传递给 MainMenu 。这些类都不应该创建自己的实例或 Kingdomcraft 屏幕的实例

What you should be doing is passing a reference of Kingdomcraft to Screen and then passing a reference of Screen to MainMenu. None of these classes should be creating instances of themselves or instance of Kingdomcraft or Screen

链中的每个链接都应该能够提供对前一个链接的访问,这样 MainMenu 应该可以询问屏幕,如果需要,可以参考 Kingdomcraft

Each link in the chain should be capable of providing access to the previous link, so that MainMenu should be able to ask Screen for a reference to Kingdomcraft should it need it.

此外,Swing是不是线程安全的。您永远不应该与事件调度线程以外的任何线程中的任何组件进行交互。

Also, Swing is not thread safe. You should never be interacting with any component from any thread other than the Event Dispatching Thread.

请参阅 Swing中的并发了解更多细节。

从外观上看,我认为你需要回到基础,看看对象引用是如何工作的......

From the looks of things, I think you need to go back to basics and look into how object referencing works...

这篇关于当我将程序设置为全屏时,为什么JPanel无法正确呈现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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