Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.ButtonGroup;
- import javax.swing.JButton;
- import javax.swing.JCheckBox;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.JRadioButton;
- import javax.swing.border.EtchedBorder;
- import javax.swing.border.TitledBorder;
- import javax.swing.UIManager;
- import javax.swing.UnsupportedLookAndFeelException;
- //hangman Frame
- public class HangmanGameFrame extends JFrame
- {
- //variables
- private JLabel secretWordLabel;
- private JLabel livesLabel;
- private HangmanGame myGame;
- private ActionListener listener;
- char letter;
- //constructor
- public HangmanGameFrame()
- {
- listener = new ChoiceListener();
- myGame = new HangmanGame("university");
- secretWordLabel = new JLabel("show secret word here");
- Font font1 = new Font("Courier", Font.BOLD,68);
- //set font for JLabel
- secretWordLabel.setFont(font1);
- createGamePanel();
- //Show the Dashes
- //String s = myGame.showSecretWord();
- String s = myGame.showDashes();
- secretWordLabel.setText(s);
- //call helper method
- }
- //Creates the control .
- public void createGamePanel()
- {
- // Add panels to content pane
- JPanel gamePanel = new JPanel();
- gamePanel.setLayout(new GridLayout(2,2));
- gamePanel.add(createSecretWordPanel());
- gamePanel.add(createKeyboardPanel());
- gamePanel.add(createHangManPanel());
- gamePanel.add(lives());
- add(gamePanel);
- }
- //Secret wordpanel
- public JPanel createSecretWordPanel()
- {
- JPanel secretWordPanel = new JPanel();
- secretWordPanel.add(secretWordLabel);
- secretWordPanel.setBorder (new TitledBorder(new EtchedBorder(), "Secret Word "));
- return secretWordPanel;
- }
- //Keyboard panel
- public JPanel createKeyboardPanel()
- {
- JPanel keyboardPanel = new JPanel();
- JButton[] buttonArray = new JButton[26];// keyboard array
- char letter = 'A';
- for(int i = 0; i < buttonArray.length; i++)
- {
- buttonArray[i] = new JButton("" + letter);
- letter++;
- buttonArray[i].addActionListener(listener);// lisner for keyboard buttons
- }
- for(int i = 0; i < buttonArray.length; i++)
- {
- keyboardPanel.add(buttonArray[i]);
- }
- keyboardPanel.setBorder(new TitledBorder(new EtchedBorder(), "Keyboard"));
- return keyboardPanel;
- }
- //hangman drawing panel
- public JPanel createHangManPanel()
- {
- JPanel hangManPanel = new JPanel();
- hangManPanel.setBorder (new TitledBorder(new EtchedBorder(), "HangMan"));
- return hangManPanel;
- }
- //Score panel
- public JPanel lives()
- {
- JPanel panel = new JPanel();
- livesLabel = new JLabel("Lives: ");
- Font font = new Font("Courier", Font.BOLD,20);
- //set font for JLabel
- livesLabel.setFont(font);
- panel.add(livesLabel);
- return panel;
- }
- //listener
- class ChoiceListener implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- boolean found = myGame.guessLetter(letter);// I called in helper method here from the hangman class
- char letter = event.getActionCommand().charAt(0);// this is the action command that suppose to make buttons work
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement