I am creating a Blackjack card game through Java. The button I created is supposed to call the main method from the Blackjack class in order to start the game.
My problem is, when pressed the button stays blue and I am unable to type into the pop-up window. This is the Window class, the ActionPerformed method is supposed to call the Blackjack class.
import java.awt.*;
import javax.swing.*;
import java.awt.Font;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.event.*;
public class exampleWindow implements ActionListener
{
public void createWindow()
{
JFrame frame = new JFrame("Blackjack");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel textLabel = new JLabel("Welcome to Blackjack!", SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(800, 600));
textLabel.setFont(new Font("Zapfino", Font.BOLD, 36));
frame.getContentPane().add(textLabel, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.getContentPane().setBackground(new Color(0,132,32));
JButton start = new JButton();
frame.getContentPane().add(BorderLayout.SOUTH, start);
start.setText("Start Game");
start.addActionListener(this);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
}
public void actionPerformed(ActionEvent e)
{
Blackjack.main(null);
}
public static void main(String[] args)
{
exampleWindow e = new exampleWindow();
e.createWindow();
}
}
Here is the main class, Blackjack:
import java.util.Scanner;
public class Blackjack
{
public static void main(String[] args)
{
System.out.println("Welcome to Blackjack! Please enter your name:");
Scanner name = new Scanner(System.in);
String playerName = name.nextLine();
System.out.println("Place your bet: ");
Scanner bet = new Scanner(System.in);
int playerBet = bet.nextInt();
int card1 = (int) Math.ceil(Math.random() * 11);
int card2 = (int) Math.ceil(Math.random() * 11);
int card3 = (int) Math.ceil(Math.random() * 11);
int card4 = (int) Math.ceil(Math.random() * 11);
System.out.println("Dealing Cards...");
System.out.println(playerName + " has: " + card1 + " and " + card2);
System.out.println("Dealer has: " + card3 + " and " + card4);
Scanner answer = new Scanner(System.in);
int playerTotal = card1 + card2;
int dealerTotal = card1 + card2;
String WoL = "";
String DWoL = "";
int stop = 0;
if (playerTotal == 21)
{
System.out.println("BlackJack! You win: " + 2 * playerBet);
}
while (stop == 0)
{
System.out.println("Hit or Stand? (H/S)");
String HoS = answer.nextLine();
if(HoS.equals("h") || HoS.equals("H"))
{
playerTotal += (int) Math.ceil(Math.random() * 11);
if(playerTotal > 21)
{
System.out.println(playerName + " has: " + playerTotal + " --> Bust! You Lose: -" + playerBet);
WoL = "L";
stop++;
}
else if (playerTotal == 21)
{
System.out.println(playerName + " has: " + playerTotal + " --> Blackjack! You win " + 2 * playerBet);
WoL = "W";
stop++;
}
else
{
System.out.println(playerName + " has: " + playerTotal);
}
}
else
{
stop++;
}
}
if(WoL.equals("W") || WoL.equals("L"))
{
}
else
{
while (dealerTotal <=16)
{
System.out.println("Dealer hits");
dealerTotal += (int) Math.ceil(Math.random() * 11);
if(dealerTotal > 21)
{
System.out.println("Dealer has: " + dealerTotal + " --> Bust! You Win: " + 2 * playerBet);
DWoL="L";
}
else if (dealerTotal == 21)
{
System.out.println("Dealer has: " + dealerTotal + " --> Blackjack! You lose: -" + playerBet);
DWoL="W";
}
else
{
System.out.println("Dealer has: " + dealerTotal);
}
}
if(DWoL.equals("W") || DWoL.equals("L"))
{
}
else
{
System.out.println("Dealer stands on: " + dealerTotal);
}
}
if(DWoL.equals("L") || DWoL.equals("W") || WoL.equals("W") || WoL.equals("L"))
{
}
else if (dealerTotal == playerTotal)
{
System.out.println("Tie Game! No money exchanged");
}
else if (dealerTotal > playerTotal)
{
System.out.println("Dealer has higher cards. Dealer wins! You lose: -" + playerBet);
}
else
{
System.out.println(playerName + " has higher cards. " + playerName + " wins! You win: " + 2 * playerBet);
}
}
}