-1

今天是个好日子。我在 netbeans 的一个项目中有很多 java 文件。一个文件名为 mainFile,而一些文件名为 addSale、addAttendance。在我的 mainFile.java 中,我创建了一个 actionPerformed 方法来检查是否单击了按钮。但是如果单击,我要检查的按钮位于其他 java 文件上。

我在 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;
    }
4

2 回答 2

2

是的,这是可能的,而且经常这样做,但魔鬼在细节中。通常,您将拥有一个响应用户交互的 Control 类,它与 View 类、GUI 完全分离。选项包括:

  • 给视图类一个公共addButtonXActionListener(ActionListener l)方法。
  • 给视图属性改变监听器支持,如果它子类化一个 Swing 组件,它会自动拥有这个,然后在你的 JButton 的 ActionListener 中,通常是一个匿名的内部类,通知监听器状态改变。
  • 给 Ciew 类一个 Control 实例变量并设置它。然后在JButton 的ActionListener 中,调用相应的控制方法。

编辑
例如,这是一个包含 3 个文件的小程序,1 个用于保存 JButton 的视图,1 个用于控件,以及第三个主类,只是为了让事情运行。

请注意,有两个 JButton,它们都使用 2 种不同的方式来通知外部类它们已被按下。

  1. View 类有一个公共方法,Button 1 有一个方法,public void setButton1Action(Action action)允许外部类设置 button1 的 Action,然后 Control 执行此操作,注入一个 AbstractAction,通知 Control 何时按下 button1。
  2. View 有一个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!");
         }
      }
   }
}
于 2014-05-08T21:00:58.590 回答
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());
    }
}

动作监听类动作监听类:

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)

我希望这回答了你的问题。

于 2014-05-08T22:09:54.710 回答