Variable is not being set after button Action

Darksoft

New Member
\[code\]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 windowpublic 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); }} 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(); } }\[/code\]Hi i'm having a problem that the variable "startPressed" is getting ignored or something else is happening. If the button start is pressed the variable "startPressed" is set true but the if clause in the "Game" class doesn't react to the new value. If i add System.out.println() or sleep then the if clause is recognizes the value and is giving me the output. I thought maybe there is a main problem in the programming structure or Java is to slow. Any ideas?ThanksLg Andrej
 
Back
Top