Advertisement
Guest User

Untitled

a guest
Mar 27th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package pkg611;
  6. import javax.swing.*;
  7. import java.awt.*;
  8. import java.awt.event.*;
  9.  
  10. public class GuessMyAnimal extends JFrame{
  11. private static final int FRAME_WIDTH = 650;
  12. private static final int FRAME_HEIGHT = 400;
  13.  
  14. private String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  15. private String toBeGuessedImage =("DOG");
  16.  
  17. private JButton[] keyBoardButtons;
  18.  
  19. private JButton pictureBtn;
  20.  
  21. private JPanel picturePanel;
  22. private JPanel displayPanel;
  23. private JPanel keyboardPanel;
  24.  
  25. private JTextField box1Tf;
  26. private JTextField box2Tf;
  27. private JTextField box3Tf;
  28. private ButtonHandler handler;
  29.  
  30. public GuessMyAnimal()
  31. {
  32. setTitle("Guess My Animal Name");
  33. setSize(FRAME_WIDTH, FRAME_HEIGHT);
  34. setLayout(new BorderLayout());
  35. //centers the window on the monitor
  36. setLocationRelativeTo(null);
  37. setDefaultCloseOperation(EXIT_ON_CLOSE);
  38.  
  39. ////////////////////////////////////////
  40. ///////////////picturePanel/////////////
  41. ////////////////////////////////////////
  42. //Create a panel
  43. picturePanel = new JPanel();
  44. picturePanel.setLayout(new FlowLayout());
  45. //create the button
  46. pictureBtn = new JButton();
  47. //create an instance pf image
  48. ImageIcon image = new ImageIcon("dog.jpg");
  49. //place the image on button
  50. pictureBtn.setIcon(image);
  51. //Add object into the panel
  52. picturePanel.add(pictureBtn);
  53.  
  54. //////////////////////////////////////
  55. ///////////displayPanel///////////////
  56. //////////////////////////////////////
  57. //Create a panel
  58. displayPanel = new JPanel();
  59. displayPanel.setLayout(new GridLayout(1, 3));
  60. //create textfields
  61. box1Tf = new JTextField();
  62. box2Tf = new JTextField();
  63. box3Tf = new JTextField();
  64. box1Tf.setEditable(false);
  65. box2Tf.setEditable(false);
  66. box3Tf.setEditable(false);
  67. displayPanel.add(box1Tf);
  68. displayPanel.add(box2Tf);
  69. displayPanel.add(box3Tf);
  70.  
  71. /////////////////////////////////
  72. ////////keyboardPanel////////////
  73. /////////////////////////////////
  74. //Create a panel
  75. keyboardPanel = new JPanel();
  76. keyboardPanel.setLayout(new GridLayout(2, 13));
  77. //Allocate 26 x 4 byte array space
  78. keyBoardButtons = new JButton[26];
  79. //Create 26 buttons
  80. Create26Buttons();
  81. //Add buttons into the keyboard panel
  82. Add26ButtonsToPanel();
  83.  
  84. //add panels to frame
  85. add(picturePanel, BorderLayout.NORTH);
  86. add(displayPanel, BorderLayout.CENTER);
  87. add(keyboardPanel, BorderLayout.SOUTH);
  88.  
  89. setVisible(true);
  90. }
  91. public void Create26Buttons()
  92. {
  93. String letter;
  94.  
  95. handler = new ButtonHandler();
  96. for(int index = 0; index < 26; index++)
  97. {
  98. letter = String.valueOf(abc.charAt(index));
  99.  
  100. //create A button
  101. keyBoardButtons[index] =
  102. new JButton(letter);
  103. keyBoardButtons[index].addActionListener(handler);
  104.  
  105.  
  106. }
  107. }
  108. private class ButtonHandler implements ActionListener
  109. {
  110. public void actionPerformed(ActionEvent e)
  111. {
  112. String letter;
  113. for(int index = 0; index < 26; index++)
  114. {
  115. if(e.getSource() == keyBoardButtons[index])
  116. {
  117. letter = keyBoardButtons[index].getText();
  118. box1Tf.setText(letter);
  119. /*In place of the line 116,
  120. *call fgameLogic method
  121. checkAnswer(letter);
  122. */
  123. break;
  124. }
  125. }
  126. }
  127. }
  128.  
  129. public void Add26ButtonsToPanel()
  130. {
  131. for(int index = 0; index < 26; index++)
  132. keyboardPanel.add(keyBoardButtons[index]);
  133. }
  134.  
  135. public static void main(String[]args)
  136. {
  137. GuessMyAnimal game = new GuessMyAnimal();
  138. }
  139.  
  140. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement