简体   繁体   中英

How do I access each individual ActionPerformed for buttons?

I want to thank Andrew Thompson for helping me get this far in the code. How do I access each individual button's actionPerformed listener?

The code is supposed to move the "ball" on the screen based on the button you press.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Lab2a extends JFrame {

Lab2a(){
    setTitle("Lab 1b - Application #2");
    Lab2Panel p = new Lab2Panel();
    add(p);
}

public static void main(String[] args){

Lab2 frame = new Lab2();
frame.setTitle("Lab2 Application # 1");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setVisible(true);
}

}

class Lab2Panel extends JPanel{
Lab2Button canvas = new Lab2Button();
JPanel panel = new JPanel();

Lab2Panel () {

setLayout(new BorderLayout());

JButton leftButton = new JButton("left");
JButton rightButton = new JButton("right");
JButton upButton = new JButton("up");
JButton downButton = new JButton("down");

panel.add(leftButton);
panel.add(rightButton);
panel.add(upButton);
panel.add(downButton);

this.add(canvas, BorderLayout.CENTER);
this.add(panel, BorderLayout.SOUTH);

leftButton.addActionListener(new Lab2MoveBallListener(canvas));
rightButton.addActionListener(new Lab2MoveBallListener(canvas));
}


}

class Lab2Button extends JPanel {
int radius = 5;
int x = -1;
int y = -1;

protected void paintComponent(Graphics g){
    if (x<0 || y<0) {
        x = getWidth() / 2 - radius;
        y = getHeight() / 2 - radius;
    }
    super.paintComponent(g);
    g.drawOval(x,y, 2 * radius, 2 * radius);

}

    public void moveLeft(){

        x -= 5;
        this.repaint();
    }

    public void moveRight(){

        x += 5;
        this.repaint();
    }

    public void moveUp(){
        y += 5;
        this.repaint();
    }

    public void moveDown(){
        y -= 5;
        this.repaint();
    }


}

class Lab2MoveBallListener implements ActionListener{
    private Lab2Button canvas;

Lab2MoveBallListener(Lab2Button canvas) {
    this.canvas = canvas;
 }

public void actionPerformed(ActionEvent e){
    canvas.moveLeft();
}

}

Since you are using 1 action listener class with 2 buttons you will have to have a way to tell which button was pressed. You can try something like this in the actionPerformed method:

 public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().matches("left"))
      System.out.print("left button pressed");
    else if(e.getActionCommand().matches("right"))
      System.out.print("right button pressed");
}

another way would be to do this is to create an anonymous class by doing this:

 buttonLeft.addActionListener(new ActionListener(){
      public void actionPerformed(){
        //left button code
      }
    });

  buttonRight.addActionListener(new ActionListener(){
    public void actionPerformed(){
      //right button code
    }
  });

In the ActionListener's actionPerformed(...) method, you can get the text of the button that's been pressed via the ActionEvent's getActionCommand() method.

Just test it to see the results:

public void actionPerformed(ActionEvent e){
    String actionCommand = e.getActionCommand();

    System.out.println("actionCommand is: " + actionCommand);
}

Now you can use this information inside of this method to do more than just write to the standard out, but I'll let you figure out the rest.

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