Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2010
    Location
    New Cross, London, UK
    Posts
    18
    Rep Power
    0

    Default Implementing JButton in this circumstance.

    Hi all; I am new to Java, and definitely new to Swing. Basically, I am working on a University assignment that involves creating a game of Higher or Lower (the card game). Very simple I know. In the below code, I am taking input at the command line to choose the outcome of the conditionals. My question is, how can I do this using JButtons. So far, I have semi designed the UI in another class, the class also is holding the ActionListeners for the UI objects. I have also instantiated (think that the right term) an instance of each class in each others class so that they can "talk". Thanks in advance.

    Java Code:
    import java.util.Scanner;
    public class CardGames
    {
        private ShowGui gui;
        private PackOfCards cardPack;
        //private CardDisplay display;
        private Scanner scan;
        private Card card;
        private Card cardTwo;
        private int winCounter;
    
        public CardGames(ShowGui guiInstance)
        {
            this.gui = guiInstance;
            cardPack = new PackOfCards();
            scan = new Scanner(System.in);
            winCounter = 0;
        }
    
        public void higherOrLower()
        {   
            //Shuffle the pack.
            cardPack.shuffleCards(2000);
            
            //Prompt the user. See chart for Logic structure.
            while (true)
            {
                System.out.println("*********************************************");
                //Get the next card.
                cardTwo = cardPack.getNextCard();
                
                //Print the name of the first card.
                card = cardPack.getNextCard();
                System.out.println("The first card is:");
                System.out.println(card.getName());
                //display.displayCard(card);
                gui.displayCard(card);
                System.out.println("Higher or Lower?");
                int userInt = scan.nextInt();
                if (userInt == 0)
                {
                    System.out.println("You selected Higher;");
                    if (card.getValue() < cardTwo.getValue())
                    {
                        System.out.println("Well done, the card was "+cardTwo.getName()+"");
                        winCounter++;
                    }
                    else
                    {
                        System.out.println("Sorry, that is not correct.");
                        winCounter--;
                    }
                }
                
                else if (userInt == 1)
                {
                    System.out.println("You selected Lower;");
                    if(card.getValue() > cardTwo.getValue())
                    {
                        System.out.println("Well done, the card was "+cardTwo.getName()+"");
                        winCounter++;
                    }
                    else
                    {
                        System.out.println("Sorry, that is not correct.");
                        winCounter--;
                    }
                }
                
                else if (userInt == 2)
                {
                    System.out.println("You have chosen to view your winnings.");
                    if (winCounter <= 0)
                    {
                        System.out.println("Sorry, your score is "+winCounter+". This does not qualify for a prize.");
                    }
                    else if (winCounter >=1 && winCounter <=3)
                    {
                        System.out.println("You have won a Tier 1 prize. Your score is "+winCounter+"");
                    }
                    else if (winCounter >=4 && winCounter <=7)
                    {
                        System.out.println("You have won a Tier 2 prize. Your score is "+winCounter+"");
                    }
                    else if (winCounter >=8)
                    {
                        System.out.println("You have won a Tier 3 prize. Your score is "+winCounter+"");
                    }
                }
                
                else
                {
                    System.out.println("Please enter a valid option.");
                }
            }
        }
    }

  2. #2
    Join Date
    Nov 2010
    Location
    New Cross, London, UK
    Posts
    18
    Rep Power
    0

    Default

    Shameless bump here.

    I am not sure that I explained myself thoroughly the first time, basically, at the moment, the while loop takes input from a Scanner, the integer that is entered into the scanner is placed in an int variable. There are then various if's and else if's that decide on the behaviour. What I need to do is somehow meet these different conditions with my JButtons. I am aware at the moment that it is an infinite loop, but I do have a menu item that invokes System.exit(0);.

    Thanks a lot.
    Sorry for the bump.

Similar Threads

  1. Help in implementing XOR functions
    By Manfizy in forum New To Java
    Replies: 3
    Last Post: 04-13-2010, 02:25 AM
  2. Implementing Rss Feeds
    By madhu_sushmi in forum Advanced Java
    Replies: 0
    Last Post: 04-02-2010, 02:14 AM
  3. Help implementing Pacman
    By scheng12 in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 10:26 PM
  4. Implementing Interface
    By mew in forum New To Java
    Replies: 4
    Last Post: 02-16-2010, 03:33 PM

Tags for this Thread

Posting Permissions

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