Java: Method wait for ActionListener in another class [closed]

我怕爱的太早我们不能终老 提交于 2021-02-11 17:23:41

问题


I am making a board game. One class contains main UI (save, load, Labels etc.), and another class contains the actual board game buttons(8x8 array of buttons).

I'd like the first class to wait until all the buttons in the second class are pressed and then continue with the program.

Is this something that I should fix with threading? If so can someone please send me in the right direction.

UPDATE

MCVE:

public class MyGUI {

JFrame frame;
Panel mainPanel;
Panel boardPanel;

    createGUI () {
        frame = new JFrame();
        mainPanel = new Panel();
        boardPanel = new Panel();

         frame.add(mainPanel);
     }

     addBoard () {
         Board board = new Board(boardPanel);
         frame.setVisibile(true);
     }

 }

----------------------------------------------------

 public class Board {

     Panel boardPanel;

    public Board (Panel boardPanel) {
         this.boardPanel = boardPanel;
     }

     public void createButtons () {

          // (create buttons with for statements - 2D array; add to boardPanel ..)
          button.addActionListener(new Action());

     }

     // Action Listener (abstract class)
     public class ActionListener implements ActionListener {
            public void actionPerformed (ActionEvent e) {
                      amountofButtonsPressed++;
                      if (amountofButtonsPressed >= amountOfButtons) {
                             // go back to the first class and continue
                      }
            }
      }
 }

回答1:


Your first class -main UI- should implement the action listener.

At some initialization point in your second class you have to register the main class as the action listener for the buttons. All your components have a method called addActionListener(), use this method an pass the first class as parameter.

public class Main implements ActionListener() {


    public Main() {
        new Class2(this);
    }

    public actionPerformed(Event e) {....}

}


public Class2 {

    ActionListener a;

    public Class2(ActionListener a) {
        this.a = a;

        ...
        ...
        button1.addActionListener(a);
        button2.addActionListener(a);

    }

}



回答2:


The easiest way would be the use of the Observer-Observable-pattern. Make the first class the Observer and the second class the Observable. Create a field to save the state of the buttons and give this Object via the update-method to the Observer.

Example:

class SecondClass extends Observable {
    private boolean allButtonsPressed = false;

    ... your code here ... 


    private List<Observer> observers = new LinkedList<Observer>();


    public void addObserver(final Observer obs) {
        observers.add(obs);
    }

    private void setObserverValues(final Object value) {
        setChanged();
        for (Observer obs : observers) {
            obs.update(this, value);
        }
    }

    public void yourFunction() {
        allButtonsPressed = true;
        setObserverValues(allButtonsPressed);
    }
}

class FirstClass implements Observer {

    public FirstClass() {
        SecondClass sc = new SecondClass(...);
        sc.addObserver(this);
    }

    @Override
    public void update(final Observable obs, 
            final Object arg) {
        Boolean allButtonsPressed = (Boolean) arg;
        ... your code here ...
    }

}



回答3:


Probably the simplest approach would be for the first class to have the logic which must happen after all the buttons are pressed in a separate, public method. The first class will then pass an instance of itself to the second class.

In the second class (where you have your action listeners for the buttons), you make a check whenever a button is pressed to see if all the buttons have been pressed. If they have, you will then make use of the instance of the first class and call the method with the logic which should follow.

In short, you would have something like so:

public class class1
...\\Pre loading
...
launchSecondClass(this, ...)
...

public class class2
... \\Event handlers
class1.postLoad...

This, I think, is the easiest approach. Another approach would be to use threads and locks to control the order of execution.



来源:https://stackoverflow.com/questions/21186265/java-method-wait-for-actionlistener-in-another-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!