Java 不同的按钮执行不同的操作

Java 不同的按钮执行不同的操作,java,swing,button,actionlistener,Java,Swing,Button,Actionlistener,我正在做一个实验,但似乎找不到任何能提供帮助的地方 我的实验是一组多个按钮,每个按钮在SwingView的文本框中打印一行单独的文本 我有多个按钮,但每个按钮都指向同一个ActionListener import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TextArea1 implements ActionListener{ JTextArea text; int numClick = 0;

我正在做一个实验,但似乎找不到任何能提供帮助的地方

我的实验是一组多个按钮,每个按钮在SwingView的文本框中打印一行单独的文本

我有多个按钮,但每个按钮都指向同一个ActionListener

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

public class TextArea1 implements ActionListener{
JTextArea text;
int numClick = 0;

public static void main(String[] args){
    TextArea1 gui = new TextArea1();
    gui.go();
    }

public void go(){
    JFrame aFrame = new JFrame();

    JPanel aPanel = new JPanel();
    JPanel aPanel2 = new JPanel();
    JPanel aPanel3 = new JPanel();
    JPanel aPanel4 = new JPanel();
    JPanel aBoard = new JPanel();

    JButton aButton = new JButton("Just Click it");
    JButton aButton1 = new JButton("1");
    JButton aButton2 = new JButton("2");
    ...
    JButton aButton9 = new JButton("9");        

    aPanel2.setBackground(Color.darkGray);
    aPanel3.setBackground(Color.darkGray);
    aPanel4.setBackground(Color.darkGray);
    aBoard.setLayout(new GridLayout(3,3));

    aBoard.add(aButton1);
    aBoard.add(aButton2);
    ...
    aBoard.add(aButton9);

    aButton.addActionListener(this);
    aButton1.addActionListener(this);
    aButton2.addActionListener(this);
    ...
    aButton9.addActionListener(this);

    text = new JTextArea(3,20);
    text.setLineWrap(true);

    JScrollPane scroller = new JScrollPane(text);
    scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

    aPanel.add(scroller);

    aFrame.getContentPane() .add(BorderLayout.EAST, aPanel2);
    aFrame.getContentPane() .add(BorderLayout.WEST, aPanel3);
    aFrame.getContentPane() .add(BorderLayout.NORTH, aPanel4);
    aFrame.getContentPane() .add(BorderLayout.SOUTH, aPanel);
    aFrame.getContentPane() .add(BorderLayout.CENTER, aBoard);

    aFrame.setSize(350,300);
    aFrame.setVisible(true);
    }

public void actionPerformed(ActionEvent ev){
    numClick++;
    text.append("button clicked " + numClick + "time(s) \n");
    }
}

这是我到目前为止写的。我已经获得了每次单击按钮时打印新文本的代码。但是代码没有区分每个按钮,所以不管是按钮1还是按钮2,如果您需要对所有的
JButton
使用相同的
ActionListener
,那么使用

aButton.setActionCommand("First");
同样,也为另一个
JButton
s设置
ActionCommands
,并在
actionPerformed
方法中,使用

if(ev.getActionCommand.equals("First"))
    // aButton was pressed as the actionCommand of it is "First"
类似地,如果s,则添加其他
s以检查是否按下了其他
JButton
s。

看一看,这允许您隔离每个按钮的功能,但也使其可用于键绑定和菜单等其他功能

public class JustClickIt extends AbstractAction {  
    public JustClickIt() {
        putValue(NAME, "Just Click It");
    }

    public void actionPerformed(ActionEvent evt) {
        // Make it happen
    }
}
然后把它应用到按钮上

JButton aButton = new JButton(new JustClickIt());

我有一个疯狂的想法:对于每个不同的按钮使用不同的A<代码> ActuistListor <代码>。编写不同的<代码> ActuistListor < /Cord>类,您要做不同的事情。考虑看<代码>((JButk)EV。GoSoCube())。在ActionPerformed方法中,调用
System.out.println(ev.getActionCommand())并检查输出。。