创建一个具有" addActionListener"的类。方法

时间:2014-11-19 17:03:23

标签: java swing actionlistener

我正在做一个做某种"文件浏览器"的课程, 在构造函数中我创建了框架,面板ecc ..但是我想对调用此类的主程序说用户已完成选择,我知道我可以调用此类中的主要静态方法,但我想创建一个动作监听器,因为我想将这个类用于不同的程序

例如,如果FileEx是我的班级:

public class FileEx()
{


public FileEx()
{
    //program that do something 
    if(done == true)
       //here i want to call the action 
} 

public void addActionListener(ActionListener ac) //i don't know if it's correct 
                                                 //but i want something like this
{

}



}

public static void main(String[] args)
{
FileEx fileex = new FileEx();
fileex.addActionListener(new ActionListener(){

       @Override
        public void actionPerformed(ActionEvent e)
        {
              //when done is true i want this block of code to be called
        }
});

}

2 个答案:

答案 0 :(得分:3)

ActionListeners仅在添加到允许添加它们的组件时才起作用,并且通过它们通知侦听器,例如JButton,JMenuItems,JComboBoxes等。我们不知道FileEx是什么类型的,或者为什么它应该接受一个ActionListener,而且更多的信息对qutie有帮助。如果要通知另一个对象发生事件,例如计算完成,请使用其他类型的侦听器,例如PropertyChangeListener。或者,您可以在模态JDialog窗口中进行处理,该窗口将通过将代码流返回到调用窗口来通知调用窗口它已完成其职责。


例如,请查看我对类似问题的回答:


修改
例如,如果您希望您的FileEx允许其他类侦听名为selection的字符串的更改(所谓的" bound"属性),您可以创建它看起来像:

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class TestFileEx {
   public static void main(String[] args) {
      final FileEx fileEx = new FileEx();

      fileEx.addPropertyChangeListener(FileEx.SELECTION, new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent evt) {
            // TODO code to call when fileEx has changed selections

            String fileExSelection = evt.getNewValue().toString();

            // or

            String fileExSelection2 = fileEx.getSelection();            

         }
      });
   }
}

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.event.SwingPropertyChangeSupport;

publicclass FileEx {
   public static final String SELECTION = "selection";
   private SwingPropertyChangeSupport propertyChangeSupport = new SwingPropertyChangeSupport(
         this);
   private String selection;

   public void someMethodThatChangesSelection() {

   }



   public String getSelection() {
      return selection;
   }

   public void setSelection(String selection) {
      String oldValue = this.selection;
      String newValue = selection;
      this.selection = selection;
      // notify the listeners of change
      propertyChangeSupport.firePropertyChange(SELECTION, oldValue, newValue);
   }

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      propertyChangeSupport.addPropertyChangeListener(listener);
   }

   public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
      propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      propertyChangeSupport.removePropertyChangeListener(listener);
   }

   public void rem(String propertyName, PropertyChangeListener listener) {
      propertyChangeSupport.removePropertyChangeListener(propertyName, listener);
   }
}

答案 1 :(得分:0)

以下是基于您的示例的代码,它添加了actionlisteners并调用它们:

public class FileEx()
{
   private final List<ActionListener> listeners = new ArrayList<>();

public FileEx()
{
    //program that do something 
    if(done == true) {
        notifyListeners();
    }
} 

public void addActionListener(ActionListener ac)
{
    listeners.add(ac);
}

private void notifyListeners()
{
    for (final ActionListener listener: listeners)
    {
        listener.actionPerformed(null);//You can create event if you want.
    }
}



}

public static void main(String[] args)
{
FileEx fileex = new FileEx();
fileex.addActionListener(new ActionListener(){

       @Override
        public void actionPerformed(ActionEvent e)
        {
              //when done is true i want this block of code to be called
        }
});

}