prprice16

blackjack starter code

Oct 15th, 2020 (edited)
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.GridLayout;
  3.  
  4. import javax.swing.*;
  5.  
  6. public class BlackjackGUI extends JFrame
  7. {
  8.     //GUI components
  9.     //the dealer card images will be represented by an array of 5 JLabel objects
  10.     private JLabel[] lblDealerCards;
  11.  
  12.     //a JLabel to display the Dealer total
  13.     private JLabel lblDealer;
  14.  
  15.     //the player card images will be represented by an array of 5 JLabel objects.
  16.     private JLabel[] lblPlayerCards;
  17.  
  18.     //a label to display the Player total
  19.     private JLabel lblPlayer;
  20.  
  21.     //the buttons for Hit, Stand, New Game
  22.     private JButton btnHit, btnStand, btnNewgame;
  23.  
  24.     //a label to display messages, status
  25.     private JLabel lblStatus;
  26.  
  27.     //panels to arrange layout
  28.     private JPanel pnlTop, pnlBottom;
  29.     private JPanel pnlPlayerCards, pnlDealerCards;
  30.     private JPanel pnlPlayer, pnlDealer;
  31.     private JPanel pnlButtons;
  32.    
  33.  
  34.     //constructor
  35.     public BlackjackGUI()
  36.     {
  37.         //set title of frame
  38.         setTitle("Blackjack!");
  39.         //build the GUI
  40.         buildPanel();
  41.         //fill the frame
  42.         add(pnlTop, BorderLayout.CENTER);
  43.         add(pnlBottom, BorderLayout.SOUTH);
  44.     }//end constructor
  45.    
  46.     private void buildPanel()
  47.     {
  48.         //instantiate components
  49.         //the dealer card images will be represented by an array of 5 JLabel objects
  50.         lblDealerCards = new JLabel[5];
  51.  
  52.         //a JLabel to display the Dealer total
  53.         lblDealer = new JLabel("Dealer total: ");
  54.  
  55.         //the player card images will be represented by an array of 5 JLabel objects.
  56.         lblPlayerCards= new JLabel[5];
  57.  
  58.         //a label to display the Player total
  59.         lblPlayer = new JLabel("Player total:");
  60.  
  61.         //the buttons for Hit, Stand, New Game
  62.         btnHit = new JButton("Hit");
  63.         btnStand = new JButton("Stand");
  64.         btnNewgame = new JButton("New game");
  65.  
  66.         //a label to display messages, status
  67.         lblStatus = new JLabel("messages here");
  68.  
  69.         //the dealer cards will be added to pnlDealerCards using flow layout
  70.         pnlDealerCards = new JPanel();
  71.         //the JLabel objects need to be created
  72.         for (int i = 0; i < lblDealerCards.length; i++)
  73.         {  
  74.             //make object
  75.             lblDealerCards[i] = new JLabel("");
  76.             //add to panel
  77.             pnlDealerCards.add(lblDealerCards[i]);
  78.         }
  79.            
  80.         //set up pnlDealer to use border layout
  81.         pnlDealer = new JPanel(new BorderLayout());
  82.        
  83.         //add cards to top of pnlDealer, label to bottom
  84.         pnlDealer.add(pnlDealerCards, BorderLayout.CENTER);
  85.         pnlDealer.add(lblDealer, BorderLayout.SOUTH);
  86.        
  87.         //DO THE SAME THING FOR PLAYER
  88.         //player cards will be added to pnlPlayerCards using flow layout
  89.         pnlPlayerCards = new JPanel();
  90.         //the JLabel objects need to be created
  91.         for (int i = 0; i < lblPlayerCards.length; i++)
  92.         {  
  93.             //make object
  94.             lblPlayerCards[i] = new JLabel("");
  95.             //add to panel
  96.             pnlPlayerCards.add(lblPlayerCards[i]);
  97.         }      
  98.                
  99.         //set up pnlPlayer to use border layout
  100.         pnlPlayer = new JPanel(new BorderLayout());
  101.                
  102.         //add cards to top of pnlPlayer, label to bottom
  103.         pnlPlayer.add(pnlPlayerCards, BorderLayout.CENTER);
  104.         pnlPlayer.add(lblPlayer, BorderLayout.SOUTH);
  105.                
  106.         //top panel is a grid with pnlDealer and pnlPlayers
  107.         //2 rows, 1 column
  108.         pnlTop = new JPanel(new GridLayout(0,1));
  109.         pnlTop.add(pnlDealer);
  110.         pnlTop.add(pnlPlayer);
  111.        
  112.         //now set up the bottom panel
  113.         //first the buttons go on a panel, flow is fine
  114.         pnlButtons = new JPanel();
  115.         //add buttons
  116.         pnlButtons.add(btnHit);
  117.         pnlButtons.add(btnStand);
  118.         pnlButtons.add(btnNewgame);
  119.         //this "row" goes in pnlBottom along with lblMessage
  120.         //multiple rows, 1 column
  121.         pnlBottom = new JPanel(new GridLayout(0,1));
  122.         //add the two rows
  123.         pnlBottom.add(pnlButtons);
  124.         pnlBottom.add(lblStatus);
  125.        
  126.         //call startGame method
  127.  
  128.        
  129.        
  130.        
  131.     }//end buildPanel
  132.  
  133.    
  134.  
  135. }//end class
Add Comment
Please, Sign In to add comment