Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import javax.swing.*;
- public class BlackjackGUI extends JFrame
- {
- //GUI components
- //the dealer card images will be represented by an array of 5 JLabel objects
- private JLabel[] lblDealerCards;
- //a JLabel to display the Dealer total
- private JLabel lblDealer;
- //the player card images will be represented by an array of 5 JLabel objects.
- private JLabel[] lblPlayerCards;
- //a label to display the Player total
- private JLabel lblPlayer;
- //the buttons for Hit, Stand, New Game
- private JButton btnHit, btnStand, btnNewgame;
- //a label to display messages, status
- private JLabel lblStatus;
- //panels to arrange layout
- private JPanel pnlTop, pnlBottom;
- private JPanel pnlPlayerCards, pnlDealerCards;
- private JPanel pnlPlayer, pnlDealer;
- private JPanel pnlButtons;
- //constructor
- public BlackjackGUI()
- {
- //set title of frame
- setTitle("Blackjack!");
- //build the GUI
- buildPanel();
- //fill the frame
- add(pnlTop, BorderLayout.CENTER);
- add(pnlBottom, BorderLayout.SOUTH);
- }//end constructor
- private void buildPanel()
- {
- //instantiate components
- //the dealer card images will be represented by an array of 5 JLabel objects
- lblDealerCards = new JLabel[5];
- //a JLabel to display the Dealer total
- lblDealer = new JLabel("Dealer total: ");
- //the player card images will be represented by an array of 5 JLabel objects.
- lblPlayerCards= new JLabel[5];
- //a label to display the Player total
- lblPlayer = new JLabel("Player total:");
- //the buttons for Hit, Stand, New Game
- btnHit = new JButton("Hit");
- btnStand = new JButton("Stand");
- btnNewgame = new JButton("New game");
- //a label to display messages, status
- lblStatus = new JLabel("messages here");
- //the dealer cards will be added to pnlDealerCards using flow layout
- pnlDealerCards = new JPanel();
- //the JLabel objects need to be created
- for (int i = 0; i < lblDealerCards.length; i++)
- {
- //make object
- lblDealerCards[i] = new JLabel("");
- //add to panel
- pnlDealerCards.add(lblDealerCards[i]);
- }
- //set up pnlDealer to use border layout
- pnlDealer = new JPanel(new BorderLayout());
- //add cards to top of pnlDealer, label to bottom
- pnlDealer.add(pnlDealerCards, BorderLayout.CENTER);
- pnlDealer.add(lblDealer, BorderLayout.SOUTH);
- //DO THE SAME THING FOR PLAYER
- //player cards will be added to pnlPlayerCards using flow layout
- pnlPlayerCards = new JPanel();
- //the JLabel objects need to be created
- for (int i = 0; i < lblPlayerCards.length; i++)
- {
- //make object
- lblPlayerCards[i] = new JLabel("");
- //add to panel
- pnlPlayerCards.add(lblPlayerCards[i]);
- }
- //set up pnlPlayer to use border layout
- pnlPlayer = new JPanel(new BorderLayout());
- //add cards to top of pnlPlayer, label to bottom
- pnlPlayer.add(pnlPlayerCards, BorderLayout.CENTER);
- pnlPlayer.add(lblPlayer, BorderLayout.SOUTH);
- //top panel is a grid with pnlDealer and pnlPlayers
- //2 rows, 1 column
- pnlTop = new JPanel(new GridLayout(0,1));
- pnlTop.add(pnlDealer);
- pnlTop.add(pnlPlayer);
- //now set up the bottom panel
- //first the buttons go on a panel, flow is fine
- pnlButtons = new JPanel();
- //add buttons
- pnlButtons.add(btnHit);
- pnlButtons.add(btnStand);
- pnlButtons.add(btnNewgame);
- //this "row" goes in pnlBottom along with lblMessage
- //multiple rows, 1 column
- pnlBottom = new JPanel(new GridLayout(0,1));
- //add the two rows
- pnlBottom.add(pnlButtons);
- pnlBottom.add(lblStatus);
- //call startGame method
- }//end buildPanel
- }//end class
Add Comment
Please, Sign In to add comment