我在mainFile.java中添加了这段代码
AddSales addSaleButton;
Login logButton;
public void actionPerformed(ActionEvent ae){
if (ae.getSource() == addSaleButton.getButton()){
System.out.print("sample add");
}else if (ae.getSource() == logButton.getButton()){
System.out.print("sample log");
}
}
public void setButtonAction(Action action) {
(addSaleButton.getButton()).setAction(action);
}
然后我在addSales.java中添加了这个
public JButton getButton() {
return confirmAddSales;
}
答案 0 :(得分:2)
是的,这是可能的,并且经常完成,但魔鬼在细节中。通常,您将拥有一个Control类,它响应与View类(GUI)完全独立的用户交互。选项包括:
addButtonXActionListener(ActionListener l)
方法。 修改强>
例如,这是一个包含3个文件的小程序,1个用于保存JButton的View,1个用于Control,第3个主要类用于运行。
请注意,有两个JButton,它们都使用两种不同的方式通知他们已被按下的外部类。
public void setButton1Action(Action action)
,允许外部类设置button1的Action,然后Control执行此操作,注入一个AbstractAction,通知控件何时button1被压了。public void addPropertyChangeListener(PropertyChangeListener l)
包装器方法,允许外部类添加到PropertyChangeListener中,然后将其添加到mainPanel对象的属性更改支持中。然后在button2的匿名内部ActionListener类中,要求mainPanel的PropertyChangeSupport通知所有侦听器BUTTON2属性状态的变化。然后,View会添加一个PropertyChangeListener并侦听此属性的状态并进行响应。import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
public class TestButtonPress {
private static void createAndShowGui() {
View view = new View();
Control control = new Control();
control.setView(view);
JFrame frame = new JFrame("TestButtonPress");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(view.getMainPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class View {
public static final String BUTTON2 = "Button 2";
private JPanel mainPanel = new JPanel();
private JButton button1 = new JButton();
private JButton button2 = new JButton(BUTTON2);
public View() {
mainPanel.add(button1);
mainPanel.add(button2);
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mainPanel.firePropertyChange(BUTTON2, false, true);
}
});
}
public JComponent getMainPanel() {
return mainPanel;
}
public void setButton1Action(Action action) {
button1.setAction(action);
}
public void addPropertyChangeListener(PropertyChangeListener l) {
mainPanel.addPropertyChangeListener(l);
}
public void removePropertyChangeListener(PropertyChangeListener l) {
mainPanel.removePropertyChangeListener(l);
}
}
class Control {
View view;
public void setView(final View view) {
this.view = view;
view.setButton1Action(new ButtonAction());
view.addPropertyChangeListener(new Button2Listener());
};
private class ButtonAction extends AbstractAction {
public ButtonAction() {
super("Button 1");
}
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println(evt.getActionCommand() + " has been pressed!");
}
}
private class Button2Listener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (View.BUTTON2.equals(evt.getPropertyName())) {
System.out.println("Button 2 has been pressed!");
}
}
}
}
答案 1 :(得分:0)
根据我的理解,你想要一个类中的按钮和另一个类中的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());
}
}
ActionListener类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)
我希望这能回答你的问题。