0

我有一个模型视图控制器应用程序。该视图包含一个带有多个 JXTaskPane 的 JXTaskContainer。JXTaskPane 有一个删除按钮,可以将其从容器中删除。

假设所有通过单击按钮自动添加的 JXTaskpanes,我如何找到正确的 JXTaskPane,然后将其从容器中删除?

`enter code here`class Holder extends JFrame {

Arraylist <Section> sectionList = new ArrayList<Section>();
JPanel holderPanel = new JPanel;
JXTaskPaneContainer sectionContainer = new JXTaskPaneContainer();

this.add(holderPanel);

// here goes other stuff



 class AddSectionAction implements ActionListener{

  //actionPerformed
    Section section = new Section();
    section.addActionListener(new DeleteSectionAction);
    sectionList.add(section);
    sectionContainer.add(section);

    holderPanel.add(sectionContainer);
    holderPanel.revalidate();   
    holderPanel.repain();

 }


 class DeleteSectionAction implements ActionListener{

   //actionPerformed

   sectionContainer.remove(THE SECTION I WANT TO REMOVE ); 

 }
}


public class Section extends JXTaskPane {

   JTextArea textArea;
   JButton deleteMe;

   //other stuff here

   public JButton  getDeleteMe{
     return deleteMe;
   }
 }
4

1 回答 1

0

有很多方法可以实现这一点,可能最简单的方法是将引用传递SectionDeleteSectionAction侦听器

public class DeleteSectionAction implements ActionListener{
    private Section section;
    public DeleteSectionAction(Section section) {
        this.section = section;
    }

    public void actionPerformed(ActionEvent evt) {
        // Personally, I'd have a "removeSection" method in
        // the container that would also remove the
        // section from the array list...
        sectionContainer.remove(section); 
    }

 }
于 2012-10-14T19:47:45.597 回答