Results 1 to 2 of 2
  1. #1
    yasmin k is offline Member
    Join Date
    Mar 2009
    Posts
    23
    Rep Power
    0

    Exclamation guessing game using GUI

    Hello

    i am trying to create a guessing but using GUI. below i have first created a J Frame with a panel and a button that says to "click me", this works fine, after action listener, i then create a second frame and within that frame i have three button, 'click high'. 'click low', 'correct', (if guess is correct).

    But i find it difficult to PASS A BUTTON to an if statement method so that it can give a higher value, so for instance if i click high, it should able to give me a higher value, if i click low it should give a lower value from the previous value.

    i would really appreciate if you could help and guide me, i'm not expecting to give a full answer but a clear explanation for where i got wrong, and what i shold do to fix this.

    thank you for your time :)



    import javax.swing.*;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Scanner;
    public class GEUSSGUI {

    public static void main(String[] args)
    {


    JFrame frame = new JFrame("Guessing Game");
    frame.setVisible(true);
    frame.setSize(200,200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    JPanel panel = new JPanel();
    frame.add(panel);
    JButton button = new JButton("click");
    panel.add(button);
    button.addActionListener(new Action());
    }

    static class Action implements ActionListener{
    public void actionPerformed (ActionEvent e){

    JFrame frame2 = new JFrame("Guessing Game");
    frame2.setVisible(true);
    frame2.setSize(200,200);

    JPanel panel2 = new JPanel();
    frame2.add(panel2);

    int max, min, guess;
    boolean guessed;
    guessed=false;
    max = 1000;
    min = 0;
    guess = (0+1000)/2;



    JButton buttonHigh = new JButton("click high");

    if (buttonHigh)
    {
    min=guess;
    guess=(min+max)/2;

    JButton buttonLow = new JButton("click low");

    if ( buttonLow)
    {
    max=guess;
    guess=(max+min)/2;

    JButton buttonCorrect = new JButton("correct");

    if (buttonCorrect)
    guessed = true;

    panel2.add(buttonHigh);
    panel2.add( buttonLow);
    panel2.add(buttonCorrect);
    }
    }
    }
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You need to give your high/low/correct buttons actionlisteners just as you do for your first button.

    Also, please use code tags when posting code (see my signature for more info).

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. can someone help me with this game??
    By amalina in forum New To Java
    Replies: 1
    Last Post: 08-24-2009, 05:59 AM
  3. Game 21
    By aRTx in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2009, 12:33 AM
  4. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM
  5. game
    By amith in forum AWT / Swing
    Replies: 0
    Last Post: 05-19-2008, 05:16 PM

Posting Permissions

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