Java 如何访问为按钮执行的每个操作?

Java 如何访问为按钮执行的每个操作?,java,swing,awt,actionlistener,Java,Swing,Awt,Actionlistener,我要感谢安德鲁·汤普森,他帮助我在代码方面走了这么远。 如何访问每个按钮的actionPerformed侦听器 代码应该根据您按下的按钮移动屏幕上的“球” import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Lab2a extends JFrame { Lab2a(){ setTitle("Lab 1b - Application #2"); Lab2Panel p =

我要感谢安德鲁·汤普森,他帮助我在代码方面走了这么远。 如何访问每个按钮的actionPerformed侦听器

代码应该根据您按下的按钮移动屏幕上的“球”

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

}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类Lab2a扩展JFrame{
Lab2a(){
setTitle(“实验室1b-应用程序2”);
Lab2Panel p=新的Lab2Panel();
加(p);
}
公共静态void main(字符串[]args){
Lab2帧=新的Lab2();
frame.setTitle(“Lab2应用程序#1”);
frame.setLocationRelativeTo(空);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。设置尺寸(600400);
frame.setVisible(true);
}
}
类Lab2Panel扩展了JPanel{
Lab2Button画布=新的Lab2Button();
JPanel面板=新的JPanel();
Lab2Panel(){
setLayout(新的BorderLayout());
JButton leftButton=新JButton(“左”);
JButton rightButton=新JButton(“右”);
JButton upButton=新JButton(“up”);
JButton downButton=新JButton(“down”);
panel.add(左键);
面板。添加(右键);
面板。添加(向上按钮);
面板。添加(向下按钮);
添加(画布、边框布局、中心);
此.add(面板,BorderLayout.SOUTH);
addActionListener(新的Lab2MoveBallListener(画布));
addActionListener(新的Lab2MoveBallListener(画布));
}
}
类Lab2Button扩展了JPanel{
int半径=5;
int x=-1;
int y=-1;
受保护组件(图形g){

如果在ActionListener的
actionPerformed(…)
方法中使用(x),则可以通过ActionEvent的
getActionCommand()
方法获取按钮的文本

只需对其进行测试即可查看结果:

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

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

现在,您可以在这个方法中使用这些信息来做更多的事情,而不仅仅是写入标准输出,但我将让您了解其余的部分。

在ActionListener的
actionPerformed(…)
方法中,您可以通过ActionEvent的
getActionCommand()
方法获取按钮的文本

只需对其进行测试即可查看结果:

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

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

现在,您可以在这个方法中使用这些信息来完成更多的工作,而不仅仅是写入标准输出,但我将让您了解其余的工作。

因为您使用的是一个包含两个按钮的action listener类,所以您必须有一种方法来判断按下了哪个按钮。您可以在actionPerformed方法中尝试类似的操作:

 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");
}
另一种方法是通过以下操作创建匿名类:

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

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

由于您使用的是带有2个按钮的1个action listener类,因此您必须有一种方法来判断按下了哪个按钮。您可以在actionPerformed方法中尝试以下操作:

 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");
}
另一种方法是通过以下操作创建匿名类:

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

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

这是有效的,如果您不喜欢使用匹配项,您甚至可以使用
if(e.getActionCommand().equals(“left”)
。这是有效的,如果您不喜欢使用匹配项,您甚至可以使用
if(e.getActionCommand().equals(“left”)