简体   繁体   中英

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.

And then in my Screen class:

if(mm.isFullscreen) {

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

        repaint();
    }

With mm being a deceleration of MainMenu. When mm.isFullscreen is false the Screen is its normal size.

I think I read somewhere that ActionListeners can't change the value of something outside the ActionListener...?

edit:

I have fixed the isue thanks for the help but this is new code:

In the screen class:

public void setFullscreen() {

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

    repaint();
}

In the mainMenu class:

fullscreen.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    if(isFullscreen) {

                            isFullscreen = false;
                        }


                        if(!isFullscreen) {

                            isFullscreen = true;

                            screen.setFullScreen();
                }
            });

When setting isFullscreen = false; you are not changing the state of full screen window!

You may use GraphicsDevice#setFullScreenWindow(null); to set it back to windowed mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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