将“ X”移动到随机JButton

 收藏

大家好,谢谢您的帮助。我在第一个JButton上放了一个“ X”,我需要做的是这个…… 用户单击带有“ X”的按钮后,它需要消失并重新出现在其他按钮上。我认为此过程需要重复10次,因此需要某种循环。不太确定如何执行此操作。到目前为止,这是我的代码...我知道我很长一段时间都没有使用数组或列表:/

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);
    }
}
回复
  • W@无情 回复

    当然。我很确定是有一件事导致了所有24个错误...

    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
    

  • enemo 回复

    因此,如果您不介意我问...这会是什么样。我对Java还是很陌生,还没有很多概念。

  • 忆Oo红颜 回复

    好吧,回去先使用数组。这样,您将能够: 单击包含x的按钮后,将其标签更改为“”,生成一个介于0和按钮数组大小之间的随机数,当您获得该数字时,请更改该位置的按钮的标签数组中的“ X”。您可以在actionPerformed方法中执行此算法。我希望这足够清楚。

  • xid 回复

    您首先要做的关键是将所有这些b1-b48变量替换为一个数组-然后您将可以随机选择一个,而且您的代码将短很多,因为您可以替换其中的每个块48条几乎相同的线,并在阵列中有一个微小的循环。

  • 花自毁 回复

    我试图将所有内容放入数组并循环,但是每次都会出现一个错误,指出“类型的非法开始”及其将我定向到我的for循环,我无法弄清楚。

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

  • 衬托去爱 回复

    考虑数组引用b [0]可以与变量b0相同,b [1]与b1相同,依此类推。 其中b是JButton的数组

  • 水晶 回复

    我每次都会收到一条错误消息,指出“类型非法开始”

    您可以发布编译器错误消息的全文吗?它应该显示源代码行,并在代码行下面的位置加上^字符。

  • 唯一的幸福 回复

    看起来代码不在方法中。