tags:

views:

191

answers:

3
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Random;
/**
 *
 */
public class GUI
{
    private static final int BTN_MAX = 8;


    private JFrame frame;
    private JPanel panel;
    private JPanel scores;
    private JButton[] buttons;
    private ImageIcon[] icons;

    private Random rand;

    private ImageIcon beach;
    private ImageIcon lips;
    private ImageIcon discoball;
    private ImageIcon flowers;
    private ImageIcon blank;




    /**
     *
     */
    public GUI()
    {        
        beach = new ImageIcon("beach.jpg");
        lips = new ImageIcon("lips.jpg");
        discoball = new ImageIcon ("discoball.jpg");
        flowers = new ImageIcon ("flowers.jpg");
        blank = new ImageIcon ("blank.jpg");

        buttons = makeButtons();
        rand = new Random();
        startingCondition();

        icons = new ImageIcon[] { beach, lips, discoball, lips, beach, flowers, discoball, flowers};


        makeFrame();
        makeMenuBar(frame);        
        frame.pack();
        frame.setVisible(true);


    }

    /**
     * Makes the frame for the gui, inclusive of adding all components.
     */
    private void makeFrame()
    {
        int horizGap = 25; // Using this for spaces between the grid layout components
        int vertGap = 25;  // Using this for the spaces between the grid layout componeents

        frame = new JFrame("Noughts and Crosses");
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(4,2));//set layout of frame to BorderLayout

        for (int i = 0; i < BTN_MAX; i++){
            contentPane.add(buttons[i]);
        }
    }

    /**
     * 
     */
    private void makeMenuBar(JFrame frame)
    {             
        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);

        JMenu menu;
        JMenuItem item;

        menu = new JMenu("File");
        menubar.add(menu);

        item = new JMenuItem("Reset Entire Game");
            item.addActionListener(new ActionListener() 
            {
               public void actionPerformed(ActionEvent e)
               { 
               }
            });
        menu.add(item);

        item = new JMenuItem("Reset This Game");
            item.addActionListener(new ActionListener() 
            {
               public void actionPerformed(ActionEvent e) 
               { 
               }
            });
        menu.add(item);    

        item = new JMenuItem("Quit");
            item.addActionListener(new ActionListener() 
            {
               public void actionPerformed(ActionEvent e) 
               { 
               }
            });
        menu.add(item);            

        frame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {

            }
        });  

        menu = new JMenu("About");
        menubar.add(menu); 

        item = new JMenuItem("About The Game");
            item.addActionListener(new ActionListener() 
            {
               public void actionPerformed(ActionEvent e) 
               { 
               }
            });
        menu.add(item);
    }    


    /**
     * 
     */
    private JButton[] makeButtons()
    {
        final JButton button[] = new JButton[8];
        for (int i = 0; i < BTN_MAX; i++)
        {
            button[i] = new JButton("");
            button[i].setPreferredSize(new Dimension(200, 100));
            final int tmp = i;
            button[i].addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {  
                    checkForPair();
                    takeGo();
                }
            });
        }
        return button;
    }

    /**
     * 
     */
    private void startingCondition()
    {
        for (int i = 0; i < 8; i++){
            buttons[i].setIcon(blank);
        }
    }


    /**
     * 
     */
    public int returnButtonNumber()
    {
        return 0;
        // need to tell which button in the array has been clicked
        //so i can send a value to the takeGo method
        //appropriatley
    }  

    public ImageIcon getIcon(int number)
    {
        return icons[number];
    }

    /**
     * 
     */
    public boolean checkForPair()
    {
        return false;
    }

    /**
     * 
     */
    public void takeGo()
    {
        int i = returnButtonNumber();

        buttons[i].setIcon(getIcon(i));
    }
}

Hi, i was just wondering if someone could help me with a method to determine which button number has been pressed, then i can send this value to another method in order to display an appropriate image, all ideas are welcome, though could you please try to avoid complete code as much as possible as i want to try and do it myself, examples would be nice tho

thank you

+2  A: 

In keeping with giving you the least amount of information possible ;-)... try looking at JButton.setActionCommand()

Also, if you're going to use the same ActionListener for every button and check which was pressed, you should just instantiate the ActionListener once and add it to every button rather than creating one for every button.

MrWiggles
+1, i also would use one actionlistener for all buttons
Markus Lausberg
A: 

You could use the ActionEvent.getSource() in combination with your JButton[] buttons to find out the index of the button that triggered that actionPerformed. I hope that was cryptic enough :)

willcodejavaforfood
A: 

Use a different instance of ActionListener for each different action. It may well be the same class, constructed possibly with other things in the same parameterised method.

As in:

    addItem("Quit", quitCommand);
...
private void addItem(String text, final Runnable command) {
    JMenuItem item = new JMenuItem(text);
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) { 
            command.run();
        }
    });
    menu.add(item);  
}
Tom Hawtin - tackline