gpt4 book ai didi

java - 自定义事件监听器类

转载 作者:太空宇宙 更新时间:2023-11-04 15:19:27 24 4
gpt4 key购买 nike

我已经获得了一些部分编写的类,它们可以一起编辑 mp3 id3v1 文件的信息标签。所有信息均通过GUI显示。

我在这里感兴趣的是DisplayTagPanel类。如何使用

public void addTagPanelEventListener(TagPanelEventListener tagPanelEvent) {
}

从编辑按钮触发事件?下面是使用 addTagPanelEventListener 的类及其实现的 TagPanelEventListener 接口(interface)。

/**
* 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();
}

最佳答案

您需要如下修改 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

}


}

关于java - 自定义事件监听器类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20559251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com