按钮操作后未看到变量更改

时间:2013-03-22 22:10:11

标签: 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语句不会对新值作出反应:

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

        }

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

我想也许编程结构中存在一个主要问题,或者Java太慢了。有任何想法吗? 谢谢!

2 个答案:

答案 0 :(得分:1)

你的主要问题是你的startPressed变量没有变成volatile,因此在一个线程中更改它可能不会反映在另一个线程中。更改此设置,您将看到开始按钮正确更改此变量:

private volatile boolean startPressed = false;

你的游戏循环不应该像Swing线程规则那样飞行。为什么不使用Swing Timer(我的偏好)或者你需要自己的roll-your循环,然后在后台线程中这样做。还要考虑使startPressed成为一个“绑定”变量,一个在更改时告诉任何属性更改侦听器其状态已更改。这比不断轮询其价值要好。

另一个评论:你的代码会过度使用静态函数,如果你摆脱了大部分的静态修饰符,它们的组织会更好。

答案 1 :(得分:0)

当您执行类似

的操作时
while(running) {
    ...
}

这意味着,这个循环一遍又一遍地执行,没有什么能阻止它,它会像你的PC一样快地执行。因此,它往往会阻止其他一切。所以是的,你的程序结构就是问题所在。尝试在单独的线程中执行游戏循环。然后,该线程可以不时更新您的视图。也许考虑不时地暂停这个帖子或以某种方式安排它,所以你的视图只是每秒更新一次(或任何你想要的)。