Advertisement
Guest User

Untitled

a guest
Mar 5th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Font;
  3. import java.awt.GridLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.ButtonGroup;
  7. import javax.swing.JButton;
  8. import javax.swing.JCheckBox;
  9. import javax.swing.JComboBox;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JPanel;
  13. import javax.swing.JTextField;
  14. import javax.swing.JRadioButton;
  15. import javax.swing.border.EtchedBorder;
  16. import javax.swing.border.TitledBorder;
  17. import javax.swing.UIManager;
  18. import javax.swing.UnsupportedLookAndFeelException;
  19.  
  20.  
  21. //hangman Frame
  22. public class HangmanGameFrame extends JFrame
  23. {
  24.  
  25. //variables
  26. private JLabel secretWordLabel;
  27. private JLabel livesLabel;
  28. private HangmanGame myGame;
  29. private ActionListener listener;
  30. char letter;
  31.  
  32.  
  33.  
  34. //constructor
  35. public HangmanGameFrame()
  36. {
  37. listener = new ChoiceListener();
  38.  
  39. myGame = new HangmanGame("university");
  40. secretWordLabel = new JLabel("show secret word here");
  41. Font font1 = new Font("Courier", Font.BOLD,68);
  42. //set font for JLabel
  43. secretWordLabel.setFont(font1);
  44. createGamePanel();
  45.  
  46. //Show the Dashes
  47. //String s = myGame.showSecretWord();
  48. String s = myGame.showDashes();
  49. secretWordLabel.setText(s);
  50. //call helper method
  51.  
  52. }
  53. //Creates the control .
  54. public void createGamePanel()
  55. {
  56.  
  57.  
  58.  
  59. // Add panels to content pane
  60. JPanel gamePanel = new JPanel();
  61. gamePanel.setLayout(new GridLayout(2,2));
  62. gamePanel.add(createSecretWordPanel());
  63. gamePanel.add(createKeyboardPanel());
  64. gamePanel.add(createHangManPanel());
  65. gamePanel.add(lives());
  66.  
  67. add(gamePanel);
  68. }
  69.  
  70.  
  71. //Secret wordpanel
  72. public JPanel createSecretWordPanel()
  73. {
  74.  
  75. JPanel secretWordPanel = new JPanel();
  76. secretWordPanel.add(secretWordLabel);
  77. secretWordPanel.setBorder (new TitledBorder(new EtchedBorder(), "Secret Word "));
  78.  
  79. return secretWordPanel;
  80. }
  81.  
  82. //Keyboard panel
  83. public JPanel createKeyboardPanel()
  84. {
  85.  
  86. JPanel keyboardPanel = new JPanel();
  87.  
  88. JButton[] buttonArray = new JButton[26];// keyboard array
  89. char letter = 'A';
  90.  
  91. for(int i = 0; i < buttonArray.length; i++)
  92. {
  93. buttonArray[i] = new JButton("" + letter);
  94. letter++;
  95. buttonArray[i].addActionListener(listener);// lisner for keyboard buttons
  96. }
  97.  
  98. for(int i = 0; i < buttonArray.length; i++)
  99. {
  100. keyboardPanel.add(buttonArray[i]);
  101. }
  102.  
  103. keyboardPanel.setBorder(new TitledBorder(new EtchedBorder(), "Keyboard"));
  104. return keyboardPanel;
  105. }
  106.  
  107. //hangman drawing panel
  108. public JPanel createHangManPanel()
  109. {
  110. JPanel hangManPanel = new JPanel();
  111.  
  112. hangManPanel.setBorder (new TitledBorder(new EtchedBorder(), "HangMan"));
  113. return hangManPanel;
  114. }
  115.  
  116.  
  117. //Score panel
  118. public JPanel lives()
  119. {
  120. JPanel panel = new JPanel();
  121.  
  122. livesLabel = new JLabel("Lives: ");
  123. Font font = new Font("Courier", Font.BOLD,20);
  124. //set font for JLabel
  125. livesLabel.setFont(font);
  126. panel.add(livesLabel);
  127. return panel;
  128.  
  129. }
  130. //listener
  131. class ChoiceListener implements ActionListener
  132. {
  133.  
  134. public void actionPerformed(ActionEvent event)
  135. {
  136. boolean found = myGame.guessLetter(letter);// I called in helper method here from the hangman class
  137. char letter = event.getActionCommand().charAt(0);// this is the action command that suppose to make buttons work
  138. }
  139. }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement