Results 1 to 2 of 2
  1. #1
    pakupakuman is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Question ActionListener: No pause/program keeps running?

    Hello -

    My apologies; I haven't been here in a while tending to "real life" stuff.

    I have been working on the slow but continued development of a little guess-the-number game (three attempts to guess a random number from 1 to 10). The last time i worked on it, I implemented exception handling (if you entered text or a value other than 1 to 10).

    I'm now converting it to button input, so you don't have to type in a value. "Conversion" is not fully complete, so you'll see a button window and message windows (I'll work on text areas / fields later).

    The problem is the following: the program is currently not "pausing" to allow the user to click on a button, and just keeps running along as if you've guessed incorrectly. The problem my be in or near line 148, where the program jumps into the "process the number" method.

    Can someone provide a hint as to what is wrong or missing?


    Thanks!

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.Random;
    
    public class GTNW extends JFrame{
       private static final long serialVersionUID = 1L;
    
       public static int compNum, yourNum, tries, gStarted;
       public static String selectYN, openingMessage, instructionMessage, attemptMessage, yourNumString, windowMessage, closingMessage;
       public static String s2;
       public static JLabel label;
    
       public GTNW(){
          super("Guess The Number");
    
          setSize(600,200);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
    
          JPanel p = new JPanel();
          JPanel p2 = new JPanel();
          JPanel p3 = new JPanel(new GridBagLayout());
    
          JLabel blank = new JLabel(" ");
          p.add(blank);
    
          JButton b1 = new JButton("1");
          JButton b2 = new JButton("2");
          JButton b3 = new JButton("3");
          JButton b4 = new JButton("4");
          JButton b5 = new JButton("5");
          JButton b6 = new JButton("6");
          JButton b7 = new JButton("7");
          JButton b8 = new JButton("8");
          JButton b9 = new JButton("9");
          JButton b10 = new JButton("10");
    
          b1.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent ae){
              JOptionPane.showMessageDialog(null, "You pressed Button 1");
              yourNum = 1;
              processYourNum();
              }
          });
          b2.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent ae){
              JOptionPane.showMessageDialog(null, "You pressed Button 2");
              yourNum = 2;
              processYourNum();
              }
          });
          b3.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent ae){
              JOptionPane.showMessageDialog(null, "You pressed Button 3");
              yourNum = 3;
              processYourNum();
              }
          });
          b4.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent ae){
              JOptionPane.showMessageDialog(null, "You pressed Button 4");
              yourNum = 4;
              processYourNum();
              }
          });
          b5.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent ae){
              JOptionPane.showMessageDialog(null, "You pressed Button 5");
              yourNum = 5;
              processYourNum();
              }
          });
          b6.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent ae){
              JOptionPane.showMessageDialog(null, "You pressed Button 6");
              yourNum = 6;
              processYourNum();
              }
          });
          b7.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent ae){
              JOptionPane.showMessageDialog(null, "You pressed Button 7");
              yourNum = 7;
              processYourNum();
              }
          });
          b8.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent ae){
              JOptionPane.showMessageDialog(null, "You pressed Button 8");
              yourNum = 8;
              processYourNum();
              }
          });
          b9.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent ae){
              JOptionPane.showMessageDialog(null, "You pressed Button 9");
              yourNum = 9;
              processYourNum();
              }
          });
          b10.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent ae){
              JOptionPane.showMessageDialog(null, "You pressed Button 10");
              yourNum = 10;
              processYourNum();
              }
          });
    
          p2.add(b1);
          p2.add(b2);
          p2.add(b3);
          p2.add(b4);
          p2.add(b5);
          p2.add(b6);
          p2.add(b7);
          p2.add(b8);
          p2.add(b9);
          p2.add(b10);
    
          JLabel label = new JLabel("Press a button");
          JTextField textField = new JTextField("text field");
          p3.add(label);
    
          add(p, BorderLayout.NORTH);
          add(p3, BorderLayout.SOUTH);
          add(p2, BorderLayout.CENTER);
    
       }
    
    public static void main(String[] args){
       GTNW jw = new GTNW();
       jw.setVisible(true);
          openingMessage = "Find and defuse the bomb hidden behind a door!\nYou have 3 attempts to guess behind which door the bomb was hidden.\n\n";
          JOptionPane.showMessageDialog ( null, openingMessage );
          // game routine
          do
          {
              instructionMessage = "Guess a door number from 1 to 10.";
              JOptionPane.showMessageDialog ( null, instructionMessage );
              Random rand = new Random();
              compNum = rand.nextInt(10)+1;
    			 gStarted = 0;
              tries = 0;
              while ( tries < 3 )
              {
                  gStarted = 1;
    				  tries ++;
    				  processYourNum();   // problem here?
              } // end while
          } // end do
       while (selectYN.equals("y")); 
       System.exit(0);
       }
    
    public static int processYourNum()
    {
    	if (yourNum == compNum)
       {
          windowMessage = "Conrgatulations!!! ";
          JOptionPane.showMessageDialog( null, windowMessage );
          playAgain();
          }
          else
          {
             if ((yourNum < compNum) && (tries < 3))
             {
                 windowMessage = "Uh oh.  The secret number is higher.";
                 JOptionPane.showMessageDialog( null, windowMessage );
             }
             else
             {
                 if ((yourNum > compNum) && (tries < 3))
                 {
                     windowMessage = "Uh oh.  The secret number is lower.";
                     JOptionPane.showMessageDialog( null, windowMessage );
                 }
             }
       }
     
       if ((tries ==3) && (yourNum != compNum))
       {
          windowMessage = "KABOOOM!!!  The secret number was " + compNum + ". \n\n";
          JOptionPane.showMessageDialog( null, windowMessage );
          playAgain();
       }
    	return yourNum;
    } // end method
     
    public static void playAgain()
    {
       String ynYesSelected = "y";
       String selectYNMessage = "Would you like to play again (y/n)?";
       selectYN = JOptionPane.showInputDialog( selectYNMessage );
       if (!(ynYesSelected.equals(selectYN)))
       {
           String closingMessage = "Thanks for playing.  Goodbye!";
           JOptionPane.showMessageDialog( null, closingMessage );
           System.exit(0);  
       }
       gStarted = 0;
    	tries = 4;
    } // end method
    
    } // end class

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: ActionListener: No pause/program keeps running?

    You need to switch away from a look driven model over to an event driven one. Before, when you used your look with input inside it, the system would wait for user input before resuming (it would block). Now that isn't happening anymore because GUI input is more or less asynchronous with respect to the main application loop. The only way to 'trigger' something to happen is with event handlers. You app should do nothing until a user clicks a button; at that point execution will begin.

Similar Threads

  1. why does the program keep running?
    By jwl in forum New To Java
    Replies: 1
    Last Post: 10-04-2012, 09:27 PM
  2. Pause the program and Start
    By ki_ha1984 in forum New To Java
    Replies: 12
    Last Post: 04-17-2012, 10:55 PM
  3. Running code cant pause to run other class
    By ki_ha1984 in forum New To Java
    Replies: 5
    Last Post: 01-08-2012, 08:35 PM
  4. Help with running the program :)
    By dj_ee3 in forum New To Java
    Replies: 2
    Last Post: 02-27-2011, 11:19 PM
  5. How to stop/pause running third party exe?
    By 2bGeek in forum AWT / Swing
    Replies: 3
    Last Post: 02-28-2009, 07:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •