简体   繁体   English

按下JButton时如何更改布尔值

[英]How to change the value of a boolean when a JButton is pressed

My question states it all. 我的问题说明了一切。

Here is my code: 这是我的代码:

                fullscreen.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {

                        MainMenu.isFullscreen = true;
                    }
                });

Where fullscreen is a JButton. 全屏显示的是JButton。

And then in my Screen class: 然后在我的Screen类中:

if(mm.isFullscreen) { if(mm.isFullscreen){

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

        repaint();
    }

With mm being a deceleration of MainMenu. mm是MainMenu的减速度。 When mm.isFullscreen is false the Screen is its normal size. 如果mm.isFullscreen为false,则屏幕为正常大小。

I think I read somewhere that ActionListeners can't change the value of something outside the ActionListener...? 我想我读过某个地方,说ActionListeners无法更改ActionListener之外的东西的值...?

edit: 编辑:

I have fixed the isue thanks for the help but this is new code: 我已经修复了isue感谢您的帮助,但这是新代码:

In the screen class: 在屏幕类中:

public void setFullscreen() { 公共无效setFullscreen(){

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

    repaint();
}

In the mainMenu class: 在mainMenu类中:

fullscreen.addActionListener(new ActionListener() { fullscreen.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e) {

                    if(isFullscreen) {

                            isFullscreen = false;
                        }


                        if(!isFullscreen) {

                            isFullscreen = true;

                            screen.setFullScreen();
                }
            });

When setting isFullscreen = false; 当设置isFullscreen = false; you are not changing the state of full screen window! 您没有更改全屏窗口的状态!

You may use GraphicsDevice#setFullScreenWindow(null); 您可以使用GraphicsDevice#setFullScreenWindow(null); to set it back to windowed mode. 将其设置回窗口模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM