java - 按钮操作后看不到变量变化

标签 java swing

package gameprojekt;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

//The GameWindow class holds the window
public class Game extends JFrame {

    /*Global variable declaration*/
    private int width;
    private int height;
    private int windowXPos;
    private int windowYPos;

    public static String p1 = "Player1";
    public static String p2 = "Player2";

    public static int playerScore = 0;
    public static int oponentScore = 0;

    public static int player1X;
    public static int Player1Y;
    public static int player2X;
    public static int Player2Y;

    private static boolean running = true;

    public static int status = 0;
    public static JFrame frame = new JFrame("Pong"); 
    //public TestDrawPanel testPanel = new TestDrawPanel();

    public static int getStatus() {
        return status;
    }

    public static void setStatus(int status) {
        Game.status = status;
    }

    // ------------------------------------------------------------

    /**
     * Creates a new JFrame window with the given size and
     * center it based on the screen resolution
     */
    public static final long serialVersionUID = 1L;

      public Game() {
        /*Local variable declaration*/
        //JFrame frame = new JFrame("Pong");
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        width = (int)dim.getWidth();
        height = (int)dim.getHeight();

        windowXPos = width / 2 - (width / 2) / 2;
        windowYPos = height / 2 - (height / 2) / 2;
        // ------------------------------------------------------------

        // Set size, half of the screen resolution
        frame.setSize(width/2, height/2);
        // Allign the window to the users resolution
        frame.setLocation(windowXPos, windowYPos);
        frame.setVisible(true);
        frame.setResizable(false);
        // By exiting the window using "X" all relevant data is closed
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }


    /* zum Testen auskommentiert
    @Override
    public void paint(Graphics g) {
        System.out.println("test");
        this.drawPlayer(g);
    }*/

    /**
     * Draw the Player on the given location and with the given size
     * @param g Graphics object
     */
    public void drawPlayer(Graphics g) {

    }

    private static void gameLoop() {
        Menue m = new Menue();
        m.loadMenue(frame);

        while (running) {
            if (m.isStartPressed()) {
                System.out.println("test");
            }

        }
    }

    /**
    * Create the game and initialize the gameplay
    */
    public static void main(String[] args) {
        /*Variable declaration*/

        // ------------------------------------------------------------
        Game game = new Game();
        game.gameLoop();
    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameprojekt;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * 
 */
public class Menue {

    /* Global variable declaration */
    private int widthMenue;
    private int heightMenue;
    private String start = "Start";
    private String highscores = "Highscores";
    private boolean startPressed = false;
    public JButton bStart = new JButton(start);
    public JButton bScore = new JButton(highscores);
    // ----------------------------------------------------

    public boolean isStartPressed() {
        return startPressed;
    }

    public void setStartPressed(boolean startPressed) {
        this.startPressed = startPressed;
    }

    public int getWidthMenue() {
        return widthMenue;
    }

    public void setwidthMenue(int widthMenue) {
        this.widthMenue = widthMenue;
    }

    public int getheightMenue() {
        return heightMenue;
    }

    public void setheightMenue(int heightMenue) {
        this.heightMenue = heightMenue;
    }

    public void loadMenue(JFrame j) {
        JPanel menue = new JPanel();

        LayoutManager border = new BorderLayout();
        menue.setLayout(border);
        menue.setBackground(Color.black);

        bStart.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setStartPressed(true);
            }
        });

        menue.add(bStart, BorderLayout.LINE_START);
        menue.add(bScore, BorderLayout.LINE_END);

        j.getContentPane().add(menue);
    }
}

嗨,我遇到一个问题,变量 startPressed 似乎被忽略了。如果按下开始按钮,则变量 startPressed 将设置为 true,但此 while 循环中的 if 语句不会对新值使用react:

        while (running) {
            if (m.isStartPressed()) {
                System.out.println("test");
            }

        }

如果我在循环内添加 System.out.printlnThread.sleep,则 if 语句会识别该值并给出输出。

我想可能是编程结构有问题或者Java太慢了。有任何想法吗? 谢谢!

最佳答案

您的主要问题是您的 startPressed 变量没有变得 volatile ,因此在一个线程中更改它可能不会反射(reflect)在另一个线程中。更改此设置,您将看到开始按钮相应地更改了此变量:

private volatile boolean startPressed = false;

您的游戏循环不应该如此,因为它违背了 Swing 线程规则。为什么不使用 Swing 计时器(我的偏好),或者如果您需要自己的滚动循环,则在后台线程中执行此操作。还可以考虑使 startPressed 成为一个“绑定(bind)”变量,该变量在更改时会告诉任何属性更改监听器其状态已更改。这比不断轮询其值要好。

另一条评论:你的代码过度使用了静态,如果你去掉大部分静态修饰符,你的代码会组织得更好。

关于java - 按钮操作后看不到变量变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15580658/

相关文章:

java - 默认情况下,Java 构造函数不是公开的吗?

java - 如何对 map 进行排序

java - 如何在不影响原始 JPanel 的情况下更改 JPanel 属性?

java - 将 MouseListener 与 JComponent 关联

java - 从其他Jframe加载图像,Java

java - JTree 进度条在选择时空白

java - C++ 和 Java 对象模型之间的差异

JavaFX ScrollPane setVvalue() 未按预期工作

具有泛型参数基础的 Java 泛型参数

java - JTextArea setText(veryLongString) 花费太多时间