简体   繁体   English

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

[英]Waiting for multiple button inputs in Java Swing

Sorry from my bad english, not native speaker. 对不起,我的英语不好,不是母语。 I am working on a SimonSays Game on Java in a GUI. 我正在使用GUI在Java上进行SimonSays游戏。 Im new to coding. 我是编码新手。 I managed to make the aplication work on console, however its been a mess to make it work graphically. 我设法在控制台上使应用程序工作,但是使其以图形方式工作是一团糟。 The program compares the LinkedLists from the generated sequence (secuenciaSimon) to the one entered by the user throught buttons (secuenciaUsuarioGUI) however, the problem is that the compare method is called by a click on any button so the LinkedList from the generated Sequence by simon is larger that the one introduced by the user. 该程序将生成的序列(secuenciaSimon)中的LinkedLists与用户通过按钮(secuenciaUsuarioGUI)输入的序列进行比较,但问题是,通过单击任意按钮可以调用compare方法,因此simon可以从生成的Sequence中的LinkedList比用户介绍的更大。

Yellow Button Code 黄色按钮代码

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
}

Compare code 比较代码

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;


}

TL;DR Need to wait for "n" inputs of a button on a GUI before running more code without freezing the program. TL; DR在运行更多代码而不冻结程序之前,需要等待GUI上按钮的“ n”个输入。

Thanks 谢谢

Use an int count variable, set it to 0, and increment it with each button press. 使用int count变量,将其设置为0,并在每次按下按钮时将其递增。 Only do your action when the count is adequate, ie, when it == 3. 仅在计数足够时(即== 3时)执行操作。

For example 例如

// 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
    }
  }
});

A key concept is that you must make your code event-driven . 一个关键概念是您必须使代码成为事件驱动的 You're not creating a linear console program and so much code that used to be in for loops are no longer in for loops, but rather you will increment counters or change your object's state when an event occurs and then react to that change in state. 您不是在创建线性控制台程序,并且以前在for循环中使用的代码不再在for循环中,而是当事件发生时您将增加计数器或更改对象的状态,然后对状态变化做出反应。

Note: if you're listening for the user to press a JButton, don't use a MouseListener but rather an ActionListener. 注意:如果您正在侦听用户按下JButton,请不要使用MouseListener,而应使用ActionListener。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM