Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package pkg611;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class GuessMyAnimal extends JFrame{
- private static final int FRAME_WIDTH = 650;
- private static final int FRAME_HEIGHT = 400;
- private String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- private String toBeGuessedImage =("DOG");
- private JButton[] keyBoardButtons;
- private JButton pictureBtn;
- private JPanel picturePanel;
- private JPanel displayPanel;
- private JPanel keyboardPanel;
- private JTextField box1Tf;
- private JTextField box2Tf;
- private JTextField box3Tf;
- private ButtonHandler handler;
- public GuessMyAnimal()
- {
- setTitle("Guess My Animal Name");
- setSize(FRAME_WIDTH, FRAME_HEIGHT);
- setLayout(new BorderLayout());
- //centers the window on the monitor
- setLocationRelativeTo(null);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- ////////////////////////////////////////
- ///////////////picturePanel/////////////
- ////////////////////////////////////////
- //Create a panel
- picturePanel = new JPanel();
- picturePanel.setLayout(new FlowLayout());
- //create the button
- pictureBtn = new JButton();
- //create an instance pf image
- ImageIcon image = new ImageIcon("dog.jpg");
- //place the image on button
- pictureBtn.setIcon(image);
- //Add object into the panel
- picturePanel.add(pictureBtn);
- //////////////////////////////////////
- ///////////displayPanel///////////////
- //////////////////////////////////////
- //Create a panel
- displayPanel = new JPanel();
- displayPanel.setLayout(new GridLayout(1, 3));
- //create textfields
- box1Tf = new JTextField();
- box2Tf = new JTextField();
- box3Tf = new JTextField();
- box1Tf.setEditable(false);
- box2Tf.setEditable(false);
- box3Tf.setEditable(false);
- displayPanel.add(box1Tf);
- displayPanel.add(box2Tf);
- displayPanel.add(box3Tf);
- /////////////////////////////////
- ////////keyboardPanel////////////
- /////////////////////////////////
- //Create a panel
- keyboardPanel = new JPanel();
- keyboardPanel.setLayout(new GridLayout(2, 13));
- //Allocate 26 x 4 byte array space
- keyBoardButtons = new JButton[26];
- //Create 26 buttons
- Create26Buttons();
- //Add buttons into the keyboard panel
- Add26ButtonsToPanel();
- //add panels to frame
- add(picturePanel, BorderLayout.NORTH);
- add(displayPanel, BorderLayout.CENTER);
- add(keyboardPanel, BorderLayout.SOUTH);
- setVisible(true);
- }
- public void Create26Buttons()
- {
- String letter;
- handler = new ButtonHandler();
- for(int index = 0; index < 26; index++)
- {
- letter = String.valueOf(abc.charAt(index));
- //create A button
- keyBoardButtons[index] =
- new JButton(letter);
- keyBoardButtons[index].addActionListener(handler);
- }
- }
- private class ButtonHandler implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- String letter;
- for(int index = 0; index < 26; index++)
- {
- if(e.getSource() == keyBoardButtons[index])
- {
- letter = keyBoardButtons[index].getText();
- box1Tf.setText(letter);
- /*In place of the line 116,
- *call fgameLogic method
- checkAnswer(letter);
- */
- break;
- }
- }
- }
- }
- public void Add26ButtonsToPanel()
- {
- for(int index = 0; index < 26; index++)
- keyboardPanel.add(keyBoardButtons[index]);
- }
- public static void main(String[]args)
- {
- GuessMyAnimal game = new GuessMyAnimal();
- }
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement