组合多个Actionlisteners

时间:2016-12-09 02:00:22

标签: java actionlistener

如何将ActionListeners实现到执行不同任务的多个按钮?我需要他们访问这个类中的卡片,所以我试着在我的FilledFrame类中做一个类,如下所示:

类ClickListener实现了ActionListener {

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

}

然而,在每个JButton上实现这一点意味着我必须在我的原始类中创建6个新类来完成更多任务(当我添加更多按钮时可能更多)。有没有办法在原始内部创建一个类,并改变ActionListener响应的方式,以便它可以用于所有按钮?

导入javax.swing。*;

公共类FilledFrame扩展了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);

}

}

3 个答案:

答案 0 :(得分:1)

  

然而,在每个JButton上实现这一点意味着我将不得不在我的原始类中创建6个新类来执行更多任务

为什么你认为只需要实现一个接口就需要一个完整的类?

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 { }
    }
}

即使这不是一个选项,您只需要一个类来更改点击标签

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

并添加

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

答案 1 :(得分:1)

如果您使用Java8,那么thrasher建议的内容可以通过lambdas更紧凑地完成:

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

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

答案 2 :(得分:0)

您可以动态覆盖actionListener以避免创建类,如下所示:

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

注意:根据Naming Conventions

,按钮名称应以小写字母开头