Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java Swing中等待多个按钮输入_Java_Swing_User Interface_Wait - Fatal编程技术网

在Java Swing中等待多个按钮输入

在Java Swing中等待多个按钮输入,java,swing,user-interface,wait,Java,Swing,User Interface,Wait,对不起,我英语不好,母语不是英语。我正在GUI中开发一个基于Java的SimonSays游戏。我是新来的编码。我设法使应用程序在控制台上工作,但使其以图形方式工作是一团混乱。该程序将生成序列(secuenciaSimon)中的链接列表与用户通过按钮(secuenciaUsuarioGUI)输入的链接列表进行比较。然而,问题是,单击任何按钮都会调用比较方法,因此simon生成序列中的链接列表比用户引入的链接列表大 黄色按钮代码 private void bAmarilloMousePressed(

对不起,我英语不好,母语不是英语。我正在GUI中开发一个基于Java的SimonSays游戏。我是新来的编码。我设法使应用程序在控制台上工作,但使其以图形方式工作是一团混乱。该程序将生成序列(secuenciaSimon)中的链接列表与用户通过按钮(secuenciaUsuarioGUI)输入的链接列表进行比较。然而,问题是,单击任何按钮都会调用比较方法,因此simon生成序列中的链接列表比用户引入的链接列表大

黄色按钮代码

private void bAmarilloMousePressed(java.awt.event.MouseEvent evt) {
   secuenciaUsuarioGUI.add(3); //Adds  the selection to the LinkedList yellow=3
   System.out.println("Secuencua Usuario GUI:" + secuenciaUsuarioGUI.toString()); 
   comparaSecuencia();
   generaSecuencia();  //Adds another value to the LinkedList
}
比较代码

public boolean comparaSecuencia(){
    for (int i = 0; i < secuenciaSimon.size(); i++) {      

            //Here the pause should be

            if(secuenciaSimon.get(i) != secuenciaUsuarioGUI.get(i)){

                System.out.println("Not equal");
                 return false;
            }

    } 
    System.out.println("Equal");
    puntuacion += 100; //Score
    secuenciaUsuarioGUI.clear(); //Clears the LinkedList From the user
    return true;


}
publicsecuencia(){
对于(inti=0;i
TL;博士 在运行更多代码而不冻结程序之前,需要等待GUI上按钮的“n”输入


谢谢

使用int count变量,将其设置为0,并在每次按下按钮时递增。仅当计数足够时才执行操作,即当计数=3时

比如说

// this is a class field
private int count = 0;

// in the code where you create your GUI
button.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent evt){
    count++;  // increment count
    // do something with the button pressed, add information to a list
    secuenciaUsuarioGUI.add(/* ?? something ?? */);
    if (count == 3) {  
      // check the sequence
      comparaSecuencia(); // ?? maybe this
      count = 0; // reset
    }
  }
});
一个关键概念是,您必须使代码由事件驱动。您并不是在创建线性控制台程序,很多用于for循环的代码不再用于for循环,而是在事件发生时增加计数器或更改对象的状态,然后对状态的更改做出反应


注意:如果您正在听用户按JButton,请不要使用鼠标侦听器,而是使用ActionListener。

听起来不错,但我希望在达到这三个按钮之前能够进行比较。例如,如果序列为红色>蓝色>蓝色,而我输入蓝色>蓝色,程序会等到最后一个按钮,还是称其为false,因为按下的第一个按钮与序列不同?@Fravo:那么您必须将此逻辑编码到程序中。但关键是您必须使代码由事件驱动。您并不是在创建线性控制台程序,所以以前用于for循环的代码不再用于for循环,而是在事件发生时增加计数器或更改对象的状态,然后对状态更改做出反应。谢谢,我通过实现您的代码示例并使用userSecuence.size添加一个额外的比较器,成功地解决了这个问题。祝您度过愉快的一天。