据我了解,您希望您的按钮在一个类中,而 ActionListener 在另一个类中。我不是 100% 确定这是否是你想要的,但这里有一些代码:
主类 Btn:
package btn;
public class Btn
{
public static void main(String[] args)
{
Frame f = new Frame();
f.setVisible(true);
}
}
按钮类按钮:
package btn;
import javax.swing.JButton;
public class Button extends JButton
{
public Button()
{
super("Hello world");
this.addActionListener(new ActionListenerClass());
}
}
动作监听类动作监听类:
package btn;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ActionListenerClass implements ActionListener
{
public static int numberOfClicks = 0;
@Override
public void actionPerformed(ActionEvent e)
{
numberOfClicks++;
System.out.println("HELLO WORLD!!\n" + "number of times pressed " + numberOfClicks);
}
}
框架类框架:
package btn;
import javax.swing.JFrame;
public class Frame extends JFrame
{
public Frame()
{
super("Hello world test");
this.setBounds(0, 0, 300, 500);
this.add(new Button());
this.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
}
}
和输出:
run:
HELLO WORLD!!
number of times pressed 1
HELLO WORLD!!
number of times pressed 2
HELLO WORLD!!
number of times pressed 3
HELLO WORLD!!
number of times pressed 4
BUILD SUCCESSFUL (total time: 3 seconds)
您在这里所做的只是将动作侦听器放在一个单独的类中,然后告诉按钮在该类中查找动作侦听器
使用这行代码:this.addActionListener(new ActionListenerClass());
希望这会有所帮助,卢克。
编辑:
尝试使用e.paramString():
这将打印如下内容:
HELLO WORLD!!
number of times pressed 1
Button: ACTION_PERFORMED,cmd=Hello world,when=1399588160253,modifiers=Button1
BUILD SUCCESSFUL (total time: 2 seconds)
或者e.getActionCommand():
这将打印按钮名称:
run:
HELLO WORLD!!
number of times pressed 1
Button: Hello world
BUILD SUCCESSFUL (total time: 4 seconds)
完整的 actionListener 代码块:
@Override
public void actionPerformed(ActionEvent e)
{
numberOfClicks++;
System.out.println("HELLO WORLD!!\n" + "number of times pressed " + numberOfClicks +
"\ne.getActionCommand();: " + e.getActionCommand()
+ "\ne.paramString();: " + e.paramString());
}
输出:
run:
HELLO WORLD!!
number of times pressed 1
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399588455144,modifiers=Button1
BUILD SUCCESSFUL (total time: 6 seconds)
第二次编辑:
好的,使用该getActionCommand();
方法,然后使用开关结构检查女巫按钮是否被按下。
我添加了另一个按钮,一个名为“LOL”,另一个名为“Hello world”,它们都使用相同的动作监听器。
按下两者的输出:
run:
HELLO WORLD!!
number of times pressed 1
e.getActionCommand();: LOL
e.paramString();: ACTION_PERFORMED,cmd=LOL,when=1399590104631,modifiers=Button1
HELLO WORLD!!
number of times pressed 2
e.getActionCommand();: LOL
e.paramString();: ACTION_PERFORMED,cmd=LOL,when=1399590106679,modifiers=Button1
HELLO WORLD!!
number of times pressed 3
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399590107665,modifiers=Button1
HELLO WORLD!!
number of times pressed 4
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399590107780,modifiers=Button1
BUILD SUCCESSFUL (total time: 5 seconds)
现在使用开关结构来区分按钮之间的区别:
@Override
public void actionPerformed(ActionEvent e)
{
numberOfClicks++;
System.out.println("HELLO WORLD!!\n" + "number of times pressed " + numberOfClicks +
"\ne.getActionCommand();: " + e.getActionCommand()
+ "\ne.paramString();: " + e.paramString());
switch(e.getActionCommand())
{
case "LOL":
System.out.println("Button \"LOL\" was clicked");
break;
case "Hello world":
System.out.println("Button \"Hello world\" was clicked");
break;
}
}
带开关输出:
run:
HELLO WORLD!!
number of times pressed 1
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399590324792,modifiers=Button1
Button "Hello world" was clicked
HELLO WORLD!!
number of times pressed 2
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399590324943,modifiers=Button1
Button "Hello world" was clicked
HELLO WORLD!!
number of times pressed 3
e.getActionCommand();: Hello world
e.paramString();: ACTION_PERFORMED,cmd=Hello world,when=1399590325089,modifiers=Button1
Button "Hello world" was clicked
HELLO WORLD!!
number of times pressed 4
e.getActionCommand();: LOL
e.paramString();: ACTION_PERFORMED,cmd=LOL,when=1399590330897,modifiers=Button1
Button "LOL" was clicked
HELLO WORLD!!
number of times pressed 5
e.getActionCommand();: LOL
e.paramString();: ACTION_PERFORMED,cmd=LOL,when=1399590331048,modifiers=Button1
Button "LOL" was clicked
BUILD SUCCESSFUL (total time: 11 seconds)
我希望这回答了你的问题。