Hi all, thanks in advance for the help. I have placed an "X" on the first JButton and what I need it to do is this...
Once the user has clicked the button with the "X" it needs to disappear and reappear on a different button. This process needs to repeat 10 times so some sort of loop is being required I believe. Not quite sure on how to do this. Here is my code so far...I know I did it the long way without using an array or list :/

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class JClickTheX extends JFrame implements ActionListener
{
    JButton b1 = new JButton();
    JButton b2 = new JButton();
    JButton b3 = new JButton();
    JButton b4 = new JButton();
    JButton b5 = new JButton();
    JButton b6 = new JButton();
    JButton b7 = new JButton();
    JButton b8 = new JButton();
    JButton b9 = new JButton();
    JButton b10 = new JButton();
    JButton b11 = new JButton();
    JButton b12 = new JButton();
    JButton b13 = new JButton();
    JButton b14 = new JButton();
    JButton b15 = new JButton();
    JButton b16 = new JButton();
    JButton b17 = new JButton();
    JButton b18 = new JButton();
    JButton b19 = new JButton();
    JButton b20 = new JButton();
    JButton b21 = new JButton();
    JButton b22 = new JButton();
    JButton b23 = new JButton();
    JButton b24 = new JButton();
    JButton b25 = new JButton();
    JButton b26 = new JButton();
    JButton b27 = new JButton();
    JButton b28 = new JButton();
    JButton b29 = new JButton();
    JButton b30 = new JButton();
    JButton b31 = new JButton();
    JButton b32 = new JButton();
    JButton b33 = new JButton();
    JButton b34 = new JButton();
    JButton b35 = new JButton();
    JButton b36 = new JButton();
    JButton b37 = new JButton();
    JButton b38 = new JButton();
    JButton b39 = new JButton();
    JButton b40 = new JButton();
    JButton b41 = new JButton();
    JButton b42 = new JButton();
    JButton b43 = new JButton();
    JButton b44 = new JButton();
    JButton b45 = new JButton();
    JButton b46 = new JButton();
    JButton b47 = new JButton();
    JButton b48 = new JButton();

    public JClickTheX()
    {
        setTitle("Click the X!");
        setSize(750, 750);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new GridLayout(8, 6));
        add(buttonPane);

        buttonPane.add(b1);
        buttonPane.add(b2);
        buttonPane.add(b3);
        buttonPane.add(b4);
        buttonPane.add(b5);
        buttonPane.add(b6);
        buttonPane.add(b7);
        buttonPane.add(b8);
        buttonPane.add(b9);
        buttonPane.add(b10);
        buttonPane.add(b11);
        buttonPane.add(b12);
        buttonPane.add(b13);
        buttonPane.add(b14);
        buttonPane.add(b15);
        buttonPane.add(b16);
        buttonPane.add(b17);
        buttonPane.add(b18);
        buttonPane.add(b19);
        buttonPane.add(b20);
        buttonPane.add(b21);
        buttonPane.add(b22);
        buttonPane.add(b23);
        buttonPane.add(b24);
        buttonPane.add(b25);
        buttonPane.add(b26);
        buttonPane.add(b27);
        buttonPane.add(b28);
        buttonPane.add(b29);
        buttonPane.add(b30);
        buttonPane.add(b31);
        buttonPane.add(b32);
        buttonPane.add(b33);
        buttonPane.add(b34);
        buttonPane.add(b35);
        buttonPane.add(b36);
        buttonPane.add(b37);
        buttonPane.add(b38);
        buttonPane.add(b39);
        buttonPane.add(b40);
        buttonPane.add(b41);
        buttonPane.add(b42);
        buttonPane.add(b43);
        buttonPane.add(b44);
        buttonPane.add(b45);
        buttonPane.add(b46);
        buttonPane.add(b47);
        buttonPane.add(b48);

      b1.addActionListener(this);
      b2.addActionListener(this);
      b3.addActionListener(this);
      b4.addActionListener(this);
      b5.addActionListener(this);
      b6.addActionListener(this);
      b7.addActionListener(this);
      b8.addActionListener(this);
      b9.addActionListener(this);
      b10.addActionListener(this);
      b11.addActionListener(this);
      b12.addActionListener(this);
      b13.addActionListener(this);
      b14.addActionListener(this);
      b15.addActionListener(this);
      b16.addActionListener(this);
      b17.addActionListener(this);
      b18.addActionListener(this);
      b19.addActionListener(this);
      b20.addActionListener(this);
      b21.addActionListener(this);
      b22.addActionListener(this);
      b23.addActionListener(this);
      b24.addActionListener(this);
      b25.addActionListener(this);
      b26.addActionListener(this);
      b27.addActionListener(this);
      b28.addActionListener(this);
      b29.addActionListener(this);
      b30.addActionListener(this);
      b31.addActionListener(this);
      b32.addActionListener(this);
      b33.addActionListener(this);
      b34.addActionListener(this);
      b35.addActionListener(this);
      b36.addActionListener(this);
      b37.addActionListener(this);
      b38.addActionListener(this);
      b39.addActionListener(this);
      b40.addActionListener(this);
      b41.addActionListener(this);
      b42.addActionListener(this);
      b43.addActionListener(this);
      b44.addActionListener(this);
      b45.addActionListener(this);
      b46.addActionListener(this);
      b47.addActionListener(this);
      b48.addActionListener(this);

      b1.setText("X");
    }

    public void actionPerformed(ActionEvent e)
    {


    }



    public static void main(String[] args)
    {
        JClickTheX mouse = new JClickTheX();
        mouse.setVisible(true);
    }
}

Recommended Answers

All 8 Replies

well, go back and use arrays first. that way, you will be able to:
once the button holding the x is clicked, change it's label to "", generate a random number between 0 and the size of your array of buttons, and when you get this number, change the the label of the button that is in that position within the array to "X". you do this algorithm inside your actionPerformed method. I hope this is clear enough.

so if you dont mind me asking...what would this look like. I'm fairly new to Java and don't have a lot of concepts under my belt yet.

The key thing that you have to do first is to replace all those b1 - b48 variables with an array - then yhou'll be able to pick one at random, plus your code will be a lot lot shorter because you can replace each block of 48 almost-identical lines with a tiny loop through the array.

Consider that the array reference b[0] can be used the same as the variable b0, b[1] as b1, etc
where b is an array of JButtons

Im trying to place everything into an array and loop but i get an error every single time stating "illegal start of type" and its directing me to my for loop and I can't figure it out.

JButton button[] = new JButton[48];
    for(int i = 0; i< button.length; i++)
    {
        button[i] = new JButton(Integer.toString(i+1));
    }

i get an error every single time stating "illegal start of type"

Can you post the full text of the compiler error message? It should show the source line and indicate where on the line with a ^ character underneath.

sure. I'm pretty sure that there's one thing causing all 24 errors...

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class JCatchTheMouse2 extends JFrame implements ActionListener
{
    JButton button[] = new JButton[48];
    for(int i = 0; i< button.length; i++)
    {
        button[i] = new JButton(Integer.toString(i+1));
    }
    Container c = getContentPane();
    c.add(p1);

    public JCatchTheMouse2()
    {
        setTitle("Click the X!");
        setSize(750, 750);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new GridLayout(8, 6));
        add(buttonPane);
        button[i].addActionListener(this);

    }

    public void actionPerformed(ActionEvent e)
    {


    }



    public static void main(String[] args)
    {
        JCatchTheMouse2 mouse = new JCatchTheMouse2();
        mouse.setVisible(true);
    }
}



JCatchTheMouse2.java:13: error: illegal start of type
    for(int i = 0; i< button.length; i++)
    ^
JCatchTheMouse2.java:13: error: ')' expected
    for(int i = 0; i< button.length; i++)
             ^
JCatchTheMouse2.java:13: error: illegal start of type
    for(int i = 0; i< button.length; i++)
                ^
JCatchTheMouse2.java:13: error: <identifier> expected
    for(int i = 0; i< button.length; i++)
                 ^
JCatchTheMouse2.java:13: error: ';' expected
    for(int i = 0; i< button.length; i++)
                  ^
JCatchTheMouse2.java:13: error: > expected
    for(int i = 0; i< button.length; i++)
                            ^
JCatchTheMouse2.java:13: error: '(' expected
    for(int i = 0; i< button.length; i++)
                                   ^
JCatchTheMouse2.java:13: error: <identifier> expected
    for(int i = 0; i< button.length; i++)
                                      ^
JCatchTheMouse2.java:13: error: illegal start of type
    for(int i = 0; i< button.length; i++)
                                        ^
JCatchTheMouse2.java:13: error: <identifier> expected
    for(int i = 0; i< button.length; i++)
                                         ^
JCatchTheMouse2.java:14: error: ';' expected
    {
     ^
JCatchTheMouse2.java:15: error: illegal start of type
        button[i] = new JButton(Integer.toString(i+1));
              ^
JCatchTheMouse2.java:15: error: ';' expected
        button[i] = new JButton(Integer.toString(i+1));
                ^
JCatchTheMouse2.java:15: error: invalid method declaration; return type required
        button[i] = new JButton(Integer.toString(i+1));
                        ^
JCatchTheMouse2.java:15: error: <identifier> expected
        button[i] = new JButton(Integer.toString(i+1));
                                                ^
JCatchTheMouse2.java:15: error: ';' expected
        button[i] = new JButton(Integer.toString(i+1));
                                                 ^
JCatchTheMouse2.java:15: error: illegal start of type
        button[i] = new JButton(Integer.toString(i+1));
                                                  ^
JCatchTheMouse2.java:15: error: <identifier> expected
        button[i] = new JButton(Integer.toString(i+1));
                                                   ^
JCatchTheMouse2.java:15: error: ';' expected
        button[i] = new JButton(Integer.toString(i+1));
                                                    ^
JCatchTheMouse2.java:15: error: illegal start of type
        button[i] = new JButton(Integer.toString(i+1));
                                                     ^
JCatchTheMouse2.java:15: error: <identifier> expected
        button[i] = new JButton(Integer.toString(i+1));
                                                      ^
JCatchTheMouse2.java:15: error: ';' expected
        button[i] = new JButton(Integer.toString(i+1));
                                                       ^
JCatchTheMouse2.java:18: error: <identifier> expected
    c.add(p1);
         ^
JCatchTheMouse2.java:18: error: <identifier> expected
    c.add(p1);
            ^
24 errors

Looks like the code is not in a method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.