简体   繁体   English

Java动作侦听器和JButton

[英]Java Action Listener and JButtons

I have a gui class which has three different JButtons linked to one ActionListener . 我有一个gui类,该类具有链接到一个ActionListener三个不同的JButtons

In the ActionListener class I want an interaction between these three JButtons in a way that allows the program to "remember" which button was clicked previously. ActionListener类中,我希望这三个JButtons之间进行交互,使程序可以“记住”先前单击的按钮。

For example: 例如:

  1. If button1 is clicked --> remember this, 如果单击button1->请记住这一点,
  2. Then when button2 gets clicked next: do a action relative to button1 being clicked. 然后,当单击下一步button2时:相对于被单击的button1进行操作。

In other words if button1 click, now inside the if clause check for button2 click. 换句话说,如果button1单击,现在在if子句中检查button2单击。

Right now getActionCommand() does not allow me to use nested if statements because it only checks the current button clicked and has no "memory". 现在, getActionCommand()不允许我使用嵌套的if语句,因为它仅检查单击的当前按钮并且没有“内存”。

public void actionPerformed(ActionEvent e){
  if (Integer.parseInt(e.getActionCommand()) == 0){
    System.out.println("This is Jug 0");
    // wont be able to print the following
    if (Integer.parseInt(e.getActionCommand()) == 1){
      System.out.println("This is Jug 1 AFTER jug 0"); //I will never be able to print this
    }
  }
}

From my understanding of what you are looking todo... 根据我对您要做什么的了解...

So one way of achieving this is to create a instance variable that is a Boolean, so will be set true is the button has been previously clicked and then you could check inside your method if that flag has been set true. 因此,实现此目标的一种方法是创建一个布尔值实例变量,因此将其设置为true,因为该按钮先前已被单击,然后您可以在方法内部检查该标记是否设置为true。 Drawback of that approach would be that the flag will always be true once it has been clicked once. 这种方法的缺点是,一旦单击该标志,该标志将始终为真。 You'll have to implement a way to reset this flag. 您必须实现一种重置此标志的方法。

private boolean hasButtonOneBeenPressed = false; //set this to true when the button has been clicked

public void actionPerformed(ActionEvent e){
  if (Integer.parseInt(e.getActionCommand()) == 0 && !hasButtonOneBeenPressed){
    System.out.println("This is Jug 0");
    // wont be able to print the following
  } else if(Integer.parseInt(e.getActionCommand()) == 1 && hasButtonOneBeenPressed){
      System.out.println("This is Jug 1 AFTER jug 0");
    } else {
      //do something else
   }
}

You've given the ActionListener behavior -- it has an actionPerformed method, but you've yet to give it state -- fields that allow it to remember where it is and what it's done. 您已经赋予ActionListener 行为 -它具有一个actionPerformed方法,但是您尚未赋予它状态 -允许其记住其位置和完成情况的字段。 A boolean field or two is likely all you need here. 这里只需要一两个布尔字段即可。 Either that or a JButton field where you place the last button pressed -- all depending on what you want to do with this information. 一个或一个JButton字段,在该字段中放置最后一个按下的按钮-取决于您要使用此信息做什么。

For instance, if you were coding a memory game, then you could have a JButton field that holds the last button pressed. 例如,如果您正在编写一个记忆游戏,那么您可以拥有一个JButton字段,该字段保留最后一个按下的按钮。 If the field is null on actionPerformed, you know that the user is pressing the first button, and so you assign it to the field. 如果actionPerformed上的字段为null,则您知道用户正在按第一个按钮,因此您将其分配给该字段。 If the field holds a button when actionPerformed is pressed, then you know the 2nd button is pressed. 如果在按下actionPerformed时该字段包含一个按钮,则您知道第二个按钮已按下。 If it's not the same as the first, you compare image icons and behave accordingly, and then set the JButton field to null. 如果与第一个不相同,则比较图像图标并相应地执行操作,然后将JButton字段设置为null。

I just assume that u want just the last button pressed, so you just need a variable to store your last button. 我只是假设您只想按下最后一个按钮,所以您只需要一个变量来存储您的最后一个按钮。 In case u want the full button pressed history, just use an array where you add your pressed buttons. 如果您想要完整按钮按下的历史记录,只需使用一个数组即可在其中添加按下按钮。

PS: This uses java 8 lambdas for the ActionListeners. PS:这将Java 8 lambdas用于ActionListeners。

private void initComponents() {
    JButton b1 = new JButton();
    JButton b2 = new JButton();
    JButton b3 = new JButton();
    b1.addActionListener((e)-> onButton1Pressed());
    b2.addActionListener((e)-> onButton2Pressed());
    b3.addActionListener((e)-> onButton3Pressed());
}

int lastButton = -1;
private void onButton1Pressed() {
    switch (lastButton) {
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        default:
            break;
    }
    lastButton = 1;
}    

private void onButton2Pressed() {
    switch (lastButton) {
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        default:
            break;
    }
    lastButton = 2;
}

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

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