自定义EventListener类

时间:2013-12-13 04:43:56

标签: java user-interface event-listener

我已经提供了一些部分编写的类,这些类一起编辑mp3 id3v1文件的信息标记。所有信息都通过GUI显示。

我感兴趣的是班级DisplayTagPanel。我如何使用

public void addTagPanelEventListener(TagPanelEventListener tagPanelEvent) {
}

从编辑按钮触发事件? 这是使用addTagPanelEventListener的类和它实现的接口TagPanelEventListener

/**
* This Panel will display the ID3 tag information for read only
* This class should:
* - Configure the textfields for read only
* - Configure the action buttons (Edit)
* - Handle actions on Edit button
* - Fire notifications to listeners on edit button
*/
@SuppressWarnings("serial")
public class DisplayTagPanel extends AbstractTagPanel {

public DisplayTagPanel(ID3v1 id3v1Tag) {
   super(id3v1Tag);
   validate();
}

@Override
protected void configureActionFields() {
   JButton edit = new JButton("Edit");
   JPanel editOptionsPanel = new JPanel(new FlowLayout());
   editOptionsPanel.add(edit);

   this.add(editOptionsPanel, BorderLayout.PAGE_END);
}

public void addTagPanelEventListener(TagPanelEventListener tagPanelEvent) {
}

@Override
protected void configureFields() {
// TODO Auto-generated method stub

}
}

package edu.pitt.cs401.assignment4;

import org.farng.mp3.id3.ID3v1;

/**
* Define events that are triggered by action on Tag Panels 
*
*/
public interface TagPanelEventListener {

/**
* The edit action 
* @param id3v1 The ID3v1 tag that was loaded from the file is passed as argument
*/
public void onEdit(ID3v1 id3v1);

/**
* Called when the save button is pressed
* @param id3v1Edit This is the edited ID3v1 tag. 
*/
public void onSave(ID3v1 id3v1Edit);

/**
* Called when the cancel button is pressed
*/
public void onCancel();
}

1 个答案:

答案 0 :(得分:0)

您需要将DisplayTagPanel类修改为下面的

@SuppressWarnings("serial")
public class DisplayTagPanel extends AbstractTagPanel {
private TagPanelEventListener tagPanelListener = null;
public DisplayTagPanel(ID3v1 id3v1Tag) {
   super(id3v1Tag);
   validate();
   tagPanelListener = new TagPanelListenerImpl();   //create class TagPanelListenerImpl which implements TagPanelListener
}

@Override
protected void configureActionFields() {
   JButton edit = new JButton("Edit");
   //Write an action Listener class, which inplements TagPanelListener interface
   //and in its actionPerformed method call edit methot of your implementation 
   edit.addActionListener(this.tagPanelListener);
   JPanel editOptionsPanel = new JPanel(new FlowLayout());
   editOptionsPanel.add(edit);

   this.add(editOptionsPanel, BorderLayout.PAGE_END);
}

public void addTagPanelEventListener(TagPanelEventListener tagPanelEvent) 
{
    this.tagPanelListener = tagPanelEvent;  //This is your listener
}

@Override
protected void configureFields() {
// TODO Auto-generated method stub

}


}