JAVA西蒙游戏

时间:2016-11-04 19:49:10

标签: java

我需要为初学Java类完成这个simon游戏。基本上游戏需要随机化顶部标签上的3个COLOR字母" R"," B"," Y"或" G"开始玩游戏玩家将按此顺序按下颜色,如果它们是正确的,它将添加另一个字母/颜色,如果它们错了,它们将丢失。我有游戏设置的主要部分,包括颜色等...我正在努力的是打印在底部标签上按下的按钮的字母,并且还使顶部标签随机化3开始。当按下按钮时,我可以让游戏在顶部标签上打印随机字母,但它不会自己启动它并且它不会添加字母?

这是我的代码:

//10/30/16
package simongame1;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.security.SecureRandom;

public class SimonGame1 extends JFrame implements ActionListener
{   
    public SecureRandom rand;
    public String randomLetters="";
    JButton[] numberButtons = new JButton[4];
    JPanel numberButtonPanel = new JPanel();
    JLabel labelTop;
    JLabel labelBottom;
    String messageTop = "";
    String messageBottom = "";
    JButton red;
    JButton green;
    JButton yellow;
    JButton blue;
    public final String letters= "RGBY";
    public JLabel labelDisplay;


//constructor?
    public SimonGame1()
    {


        super("Simon Game!");
                labelDisplay = new JLabel ("");
        setLayout(new BorderLayout());
        red = new JButton();
                green = new JButton();
                blue = new JButton();
                yellow = new JButton();
        labelTop = new JLabel("");
                //getRandomLetter();
        labelBottom = new JLabel("");
                //System.out.print (randomLetters);
                rand = new SecureRandom();


        numberButtons = new JButton[4];
        numberButtonPanel = new JPanel();
        numberButtonPanel.setLayout(new GridLayout(3,3));


        for(int i = 0; i < 0; i++)
        {
                    numberButtons[i] = new JButton();
                    numberButtonPanel.add(numberButtons[i]);
                    numberButtons[i].addActionListener(this);
        }
        numberButtonPanel.add(red);
        red.setBackground(Color.red);
        red.addActionListener(this);

                numberButtonPanel.add(blue);
                blue.setBackground(Color.blue);
                blue.addActionListener(this);

                numberButtonPanel.add(yellow);
                yellow.setBackground (Color.yellow);
                yellow.addActionListener(this);

                numberButtonPanel.add (green);
                green.setBackground (Color.green);
                green.addActionListener(this);

        add(labelTop, BorderLayout.NORTH);
        add(labelBottom, BorderLayout.SOUTH);

        add(numberButtonPanel, BorderLayout.CENTER);


                //if (messageTop = messageBottom)


}
// TODO code application logic here

    @Override
    public void actionPerformed(ActionEvent e) 
    {
                rand = new SecureRandom();

                randomLetters+= getRandomLetter();
                labelTop.setText (randomLetters);
                labelBottom.setText (letters);

                System.out.println(e.getID());
        if(e.getSource() == red)
        {
                    System.out.println("R");
        }
                else if (e.getSource()==blue)
                {
                    System.out.println ("B");
                }
                else if (e.getSource()==yellow)
                {
                    System.out.println ("Y");
                }
                else if (e.getSource()==green)
                {
                    System.out.println ("G");
                }

        }


         public char getRandomLetter()
         {

             char randChar = letters.charAt(rand.nextInt (4));
             return randChar;
         }
}

此处是测试

package simongame1;
import javax.swing.JFrame;
public class NewClass {
    public static void main(String[] args) {
      {
        SimonGame1 simpleFrame = new SimonGame1(); 
        simpleFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        simpleFrame.setSize(300, 300); 
        simpleFrame.setVisible(true);
      }

    }
}

为什么这是一次点击?如果我单击红色按钮,它会显示任何内容,但如果我点击蓝色的那个,那就是它打印出来的时候&#34; R&#34;最后...

labelBottom.setText (messageBottom);
            **//WHY IS IT ONE LETTER BEHIND?**
            System.out.println(e.getID());
    if(e.getSource() == red)
    {
                messageBottom += "R";
                //System.out.println("R");
    }
            else if (e.getSource()==blue)
            {
                messageBottom += "B";
                //System.out.println ("B");
            }
            else if (e.getSource()==yellow)
            {
                messageBottom += "Y";
                //System.out.println ("Y");
            }
            else if (e.getSource()==green)
            {
                messageBottom += "G";
                //System.out.println ("G");
            }

1 个答案:

答案 0 :(得分:0)

初始化标签时,在将其添加到面板之前,我会生成带有您需要的字母的标签,并在那里设置标签的文本。这样,当你添加它时,你肯定会有字符。现在,只调用actionPerformed方法时设置labelTop的文本,只有在按下按钮时才会调用该文本。

所以看起来应该是这样的:

public SimonGame1() {
     //....

     rand = new SecureRandom();
     labelTop = new JLabel( "" );

     //initialize with 3
     for(int i = 0; i < 3; i++)
     {
         randomLetters += getRandomLetter();
     } 
     labelTop.setText( randomLetters );

     //...

     add(labelTop, BorderLayout.NORTH);

}

也完全没有使用这段代码,因为从未满足条件,因为你将i初始化为0然后只要它小于0就说:

for(int i = 0; i < 0; i++)
{
       numberButtons[i] = new JButton();
       numberButtonPanel.add(numberButtons[i]);
       numberButtons[i].addActionListener(this);
}

编辑:

因为你添加了问题的另一部分,你问为什么它在添加你刚刚在actionPerformed方法中单击的字符之前等待迭代,这是因为你在将下一个字符添加到字符串之前设置文本。所以你的actionPerformed方法应该如下所示:

public void actionPerformed(ActionEvent e)
{

    if(e.getSource() == red)
    {
            messageBottom += "R";
            //System.out.println("R");
    }
    else if (e.getSource()==blue)
    {
         messageBottom += "B";
         //System.out.println ("B");
    }
    else if (e.getSource()==yellow)
    {
        messageBottom += "Y";
        //System.out.println ("Y");
    }
    else if (e.getSource()==green)
    {
        messageBottom += "G";
        //System.out.println ("G");
    }

    labelBottom.setText (messageBottom);
}