-1

How do i add wait for jButton1 to be click to continue?

I have this code:

while (gameStatus == Status.CONTINUE){
    ////here add wait for jButton1 click  
    sumOfDice = rollDice(); // roll dice again
}

i have done this but its not working:

        while (gameStatus == Status.CONTINUE) { 

        jButton1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e, int sumOfDice) {
                sumOfDice = rollDice();

            }
        });
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
john
  • 65
  • 3

1 Answers1

2

You're confusing linear command line programming with event-driven GUI programming, and that won't work. Instead of waiting as you're doing, you need to respond to events as they occur, and how you respond will depend on the state of your program (actually the state of your model).

Note that this won't compile:

    jButton1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e, int sumOfDice) {
            sumOfDice = rollDice();

        }
    });

since actionPerformed is an overridden method and only can take one parameter

Better would be something along the lines of:

    jButton1.addActionListener(new ActionListener() {
        private int sumOfDice = 0;

        @Override // don't forget this important annotation
        public void actionPerformed(ActionEvent e) {
            if (gameStatus == Status.CONTINUE) {
               sumOfDice = rollDice();
            }
        }
    });

For a more detailed and specific answer, consider posting more detail about your problem along with relevant code.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373