简体   繁体   中英

Combining multiple Actionlisteners

How would I implement ActionListeners to multiple buttons that do different tasks? I need them to access the cards in this class so I tried to do a class within my FilledFrame class like this:

class ClickListener implements ActionListener {

public void actionPerformed(ActionEvent event)  {
  label.setText("Red 7");
}

}

However, implementing this on each JButton means i'm going to have to create 6 new classes within my original class to do more tasks (maybe more when I add more buttons). Is there a way to just create one class inside the original and vary how the ActionListener will respond so that it can be used for all buttons?

import javax.swing.*;

public class FilledFrame extends JFrame {

static int FRAME_WIDTH = 500;
static int FRAME_HEIGHT = 100;

public FilledFrame () {
    createComponents();
    createCards();
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
}

private void createComponents() {

    JButton Player1Move7 = new JButton("Player 1: Move 7's");
    JButton Player1Move8 = new JButton("Player 1: Move 8's");
    JButton Player1Move9 = new JButton("Player 1: Move 9's");
    JButton Player2Move7 = new JButton("Player 2: Move 7's");
    JButton Player2Move8 = new JButton("Player 2: Move 8's");
    JButton Player2Move9 = new JButton("Player 2: Move 9's");

    JPanel panel = new JPanel();

    panel.add(Player1Move7);
    panel.add(Player1Move8);
    panel.add(Player1Move9);
    panel.add(Player2Move7);
    panel.add(Player2Move8);
    panel.add(Player2Move9);

    add(panel);
}

private void createCards() {
    ...
}

public static void main (String args[]) {

    FilledFrame frame = new FilledFrame();
    frame.setVisible(true);

}

}

However, implementing this on each JButton means i'm going to have to create 6 new classes within my original class to do more tasks

Why do you think you need a whole class when all you need is to do is implement an interface?

public class FilledFrame extends JFrame implements ActionListener {

    JButton player1Move7, player1Move8;

    private void createComponents() {
        // Same listener for all buttons
        player1Move7 = new JButton("Player 1: Move 7's");
        player1Move7.addActionListener(this);

        player1Move8 = new JButton("Player 1: Move 8's");
        player1Move8.addActionListener(this);
        ...
    }

    @Override
    public void actionPerformed(ActionEvent event)  {
        // Check which button
        if (e.getSource() == player1Move7) {
            label.setText("Red 7");
        } else if (e.getSource() == player1Move8) {
            ...
        } else { }
    }
}

Even if that wasn't an option, you only need one class to change the labels on click

class ClickListener implements ActionListener {

    JLabel label;
    String text;

    public ClickListener(JLabel label, String text) {
        this.label = label; 
        this.text = text;
    }

    @Override
    public void actionPerformed(ActionEvent event)  {
        this.label.setText(this.text);
    }
}

And add that with

button.addActionListener(new ClickListener(label, "Red 7")); 

if you use Java8, what thrasher suggest can be done even more compactly through lambdas:

JButton player1Move7 = new JButton().addActionListener(e ->{
            // Player1Move7 Action
        }
    });

JButton player1Move8 = new JButton().addActionListener(e ->{
            // Player1Move8 Action
        }
    });

You can override the actionListener on the fly to avoid creating classes, like this:

JButton player1Move7 = new JButton("Player 1: Move 7's");

player1Move7.addActionListener( new ActionListener() {
    @Override
    public void actionPerformed( ActionEvent e ) {
        // Player1Move7 Action
    }
});

JButton player1Move8 = new JButton("Player 1: Move 8's");

player1Move8.addActionListener( new ActionListener() { 
    @Override
    public void actionPerformed( ActionEvent e ) {
        // Player1Move8 Action
    }
});

Note: you button names should start with lower case, according to Naming Conventions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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