Question: I have a Java blackjack game which was previously played in the console and with a Scanner; I am now moving it to GUI. My
I have a Java blackjack game which was previously played in the console and with a Scanner; I am now moving it to GUI. My BlackjackGameTable class has an inner class with an actionListener, and it works fine in terms of adding new cards with the hit button, but I do not know how to use the actionListener to replace the Scanner. My code is.
public static void main(String[] args){... ....Scanner in = new Scanner(System.in); while((playerStillIn) && (!CardGame2.isBusted(g.playerhand))){ if(g.playerhand.getScore() != 21){ g.text.setText("Hit or stay?"); String choice = in.next(); if((choice.equalsIgnoreCase("h"))){ //I want to replace this with a button press CardGame2.hit(g.playerhand, g.dealerhand); g.playerAddCardToTable(new CardRender2(g.playerhand.cardlist.get(g.playerhand.getSize()-1))); g.score.setText("Your score: " + g.playerhand.getScore()); System.out.println("YOUR HAND: " + g.playerhand.cardlist); System.out.println("YOUR SCORE: " + g.playerhand.getScore()); } else if (choice.equalsIgnoreCase("s")){ //I want to replace this with a button press as well playerStillIn = false; } } ...}
the code I pasted is in the main of my class. My Actionlistener is:
class DecisionListener implements ActionListener{ public void actionPerformed(ActionEvent a){ if(a.getSource() == hitButton){ CardGame2.hit(playerhand, dealerhand); playerAddCardToTable(new CardRender2(playerhand.cardlist.get(playerhand.getSize()-1))); score.setText("Your score: " + playerhand.getScore()); System.out.println("YOU CHOSE HIT! Your Score: " + playerhand.getScore()); } else if(a.getSource() == stayButton){ System.out.println("YOU CHOSE STAY!"); } } }
and that's an inner class of the BlackjackGameTable class. How can I do this?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
