I spend about 2 hours trying to understand why my code keeps throwing runtime errors. It doesn't want to accept user inputs... I'm pretty sure my implementaion of getext is correct. Could you guys take a look at my code and help me see what I did wrong?
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
public class Mdt1 extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
JTextField twager, dcard1, dcard2, pcard1, pcard2;
public Mdt1() {
JFrame blackjack = new JFrame();
Border wwager = BorderFactory.createTitledBorder("Wager");
JPanel pwager = new JPanel();
pwager.setBorder(wwager);
JLabel lwager = new JLabel("Enter wager:");
JTextField twager = new JTextField(6);
twager.addActionListener(this);
pwager.add(lwager);
pwager.add(twager);
JPanel pplayer = new JPanel();
Border wayer = BorderFactory.createTitledBorder("Player");
pplayer.setLayout(new BoxLayout(pplayer, BoxLayout.Y_AXIS));
pplayer.setBorder(wayer);
pplayer.setSize(100, 100);
JTextField pcard1 = new JTextField(6);
pcard1.setEditable(false);
JTextField pcard2 = new JTextField(6);
pcard2.setEditable(false);
pplayer.add(pcard1);
pplayer.add(pcard2);
JPanel pdealer = new JPanel();
Border wealer = BorderFactory.createTitledBorder("Dealer");
pdealer.setLayout(new BoxLayout(pdealer, BoxLayout.Y_AXIS));
pdealer.setBorder(wealer);
pdealer.setSize(100, 100);
JTextField dcard1 = new JTextField(6);
dcard1.setEditable(false);
JTextField dcard2 = new JTextField(6);
dcard2.setEditable(false);
pdealer.add(dcard1);
pdealer.add(dcard2);
JPanel cards = new JPanel();
Border wards = BorderFactory.createTitledBorder("Cards");
cards.setBorder(wards);
cards.setSize(220, 80);
cards.add(pplayer);
cards.add(pdealer);
JPanel pendgame = new JPanel();
Border wesult = BorderFactory.createTitledBorder("Endgame");
pendgame.setBorder(wesult);
blackjack.setLayout(new BorderLayout());
blackjack.setVisible(true);
blackjack.setSize(200, 200);
blackjack.setTitle("♣ Blackjack ♠");
blackjack.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
blackjack.add(pwager, BorderLayout.NORTH);
blackjack.add(cards);
blackjack.pack();
blackjack.setResizable(false);
}
public void actionPerformed(ActionEvent e) {
String swager = twager.getText();
double wager;
int dealer1, dealer2, player1, player2;
int dealervalue1, dealervalue2, playervalue1, playervalue2;
int dealerTotal, playerTotal;
wager = Double.parseDouble(swager);
Random r=new Random();
// card faces
dealer1=r.nextInt(13) + 1;
String sdealer1 = Integer.toString(dealer1);
dealer2=r.nextInt(13) + 1;
String sdealer2 = Integer.toString(dealer2);
player1=r.nextInt(13) + 1;
String splayer1 = Integer.toString(player1);
player2=r.nextInt(13) + 1;
String splayer2 = Integer.toString(player2);
// Show dealer's card faces
switch(dealer1)
{
case 1: dcard1.setText("Ace "); break;
case 11: dcard1.setText("Jack"); break;
case 12: dcard1.setText("Queen"); break;
case 13: dcard1.setText("King"); break;
default: dcard1.setText(sdealer1); break;
}
switch(dealer2)
{
case 1: dcard2.setText("Ace"); break;
case 11: dcard2.setText("Jack"); break;
case 12: dcard2.setText("Queen"); break;
case 13: dcard2.setText("King"); break;
default: dcard2.setText(sdealer2); break;
}
// Show player's card faces
switch(player1)
{
case 1: System.out.print("Ace"); break;
case 11: System.out.print("Jack"); break;
case 12: System.out.print("Queen"); break;
case 13: System.out.print("King"); break;
default: System.out.print(splayer1); break;
}
switch(player2)
{
case 1: System.out.println("Ace"); break;
case 11: System.out.println("Jack"); break;
case 12: System.out.println("Queen"); break;
case 13: System.out.println("King"); break;
default: System.out.println(splayer2); break;
}
// Determine card values
dealervalue1 = dealer1;
switch(dealer1)
{
case 1: dealervalue1=11; break;
case 11: case 12: case 13:
dealervalue1=10; break;
} // switch
dealervalue2 = dealer2;
switch(dealer2)
{
case 1: dealervalue2=11; break;
case 11: case 12: case 13:
dealervalue2=10; break;
} // switch
dealerTotal = dealervalue1+dealervalue2;
if (dealerTotal == 22) dealerTotal = 12;
playervalue1 = player1;
switch(player1)
{
case 1: playervalue1=11; break;
case 11: case 12: case 13:
playervalue1=10; break;
} // switch
playervalue2 = player2;
switch(player2)
{
case 1: playervalue2=11; break;
case 11: case 12: case 13:
playervalue2=10; break;
} // switch
playerTotal = playervalue1+playervalue2;
if (playerTotal == 22)
playerTotal = 12;
if (playerTotal == 21)
{
wager = wager * 1.5;
JOptionPane.showMessageDialog(null, "You have a Blackjack!!! \n You won: $" + wager);
return;
}
if (playerTotal >= dealerTotal)
{
System.out.println("" + wager);
return;
}
JOptionPane.showMessageDialog(null, "You lost: $" + wager, "You lost!", JOptionPane.ERROR_MESSAGE);
System.out.println(swager);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
/**
* 2
* 2-card Blackjack game
*/
Mdt1 black = new Mdt1();
}// main
}