Java 从JPanel获取所选组件

Java 从JPanel获取所选组件,java,swing,Java,Swing,我正在通过ArrayList向我的JPanel动态添加项目。items对象基本上如下所示: public class Item { private JComponent component; private String functionality; public Item(JComponent component, String functionality) { super(); this.component = component;

我正在通过
ArrayList
向我的JPanel动态添加项目。items对象基本上如下所示:

public class Item {

    private JComponent component;
    private String functionality;

    public Item(JComponent component, String functionality) {
        super();
        this.component = component;
        this.functionality = functionality;
    }

    public JComponent getComponent() {
        return component;
    }

    public void setComponent(JComponent component) {
        this.component = component;
    }

    public String getFunctionality() {
        return functionality;
    }

    public void setFunctionality(String functionality) {
        this.functionality = functionality;
    }
}
在这里,我动态地添加了我的按钮:(如果需要,请尝试这个示例)


为什么不让你的按钮comp标签comp从JPanel扩展出来呢?我想这会解决你的很多问题。例如:

public class ButtonComp extends JPanel {

    JButton button;
    JCheckBox check = new JCheckBox();


    public ButtonComp() {

        button = new JButton("Test1");

        this.add(button);
        this.add(check);
    }
}
然后,您需要做的就是迭代项目,并查看组件中的复选框:

for (int i = 0; i < displayedItems.size(); i++) {

    if (displayedItems.get(i).check.isSelected()) {
        displayedItems.remove(i);
    }
}
for(int i=0;i

注意:尚未测试此代码。但是你应该明白这一点。

你需要遍历所有项目,检查项目复选框是否选中,如果选中,则从面板中删除项目。我将创建一个抽象的CheckableItem类,其中包含JCheckbox和组件的getter。然后,对于每个项目,如果选中该复选框,则从父项中删除该组件

public abstract class CheckableItem {
    protected JCheckbox checkbox;
    public boolean isSelected() {
        return checkbox.isSelected();
    }
    public abstract Component getComponent();
}
public class ButtonComp extends CheckableItem {
    private Panel panel;
    public void init() {
        checkbox = new JCheckbox;
        panel = new JPanel();
        panel.add(new JButton());
        panel.add(checkbox);
    }
    public Component getComponent() {
        return panel;
    }
}
然后,要跟踪您的项目,请执行以下操作:

 private List<CheckableItem> items = new ArrayList<>();
    // ...
    ButtonComp comp = new ButtonComp();
    comp.init();  
    items.add(comp);
private List items=new ArrayList();
// ...
ButtonComp comp=新ButtonComp();
comp.init();
添加项目(comp);
然后,要删除所有选中项:

Iterator<CheckbleItem> it = items.iterator();
while (it.hasNext()) {
    CheckableItem next = it.next();
    if (next.isSelected()) {
        mainPanel.remove(next.getComponent());
        it.remove();
        continue;
    }
}
Iterator it=items.Iterator();
while(it.hasNext()){
CheckableItem next=it.next();
if(next.isSelected()){
删除(next.getComponent());
it.remove();
继续;
}
}

什么是组件,是AWT组件吗?还是您自己的自定义类?我假设
JCheckBox
用于“选择”项目?如果是这样,请自己创建一个自定义组件,该组件有一个名为的方法,类似于
isSelected
。根据需要创建组件实例,并使用
isSelected
方法确定组件的“选定”状态components@OliverWatkins它是AWT组件@谢谢你的回答!是,通过复选框,我想选择应该向上移动一个插槽或删除的组件。我想直接从
ArrayList
中删除该组件,因为我稍后会在我的组件上使用它。关于如何直接从
ArrayList
?@MadProgrammer中删除的任何建议,在您提出的解决方案中,我只需确定是否选择了组件的实例。这可能适用于delete,但如果我不知道它在
数组列表中的确切“位置”,我怎么能将其向上或向下放置?谢谢您的回答!您将如何解决向上和向下的问题,因此将一个面板1向上和向下移动?你是如何得到检查的?那有点复杂。但这不再是一个摇摆不定的问题。这只是简单的arrayList操作。如果您选中了多个复选框,那么您将遇到许多问题。顺便说一句,这是签入
displayedItems.get(i).check.isSelected())
也是我的items类中的一个字段,或者如何获得此值?它是ButtonComp或LabelComp中的JCheckbox。对于ButtonComp和LabelComp,您可能需要一个超级类,就像下面的另一个答案一样。谢谢您的详细答案!此时,您将组件的代码放入构造函数中。你能把它改成一个方法void方法并把它添加到列表中吗。我真的很感激,接受你的回答!问题是您使用了构造函数来实例化组件:
public ButtonComp(){checkbox=new JCheckbox;panel=new JPanel();panel.add(new JButton());panel.add(checkbox);}
但是我想把它放到一个方法中,并将它添加到arrayList中。但我的问题是,我不知道如何做…更新我的答案,将初始化移到另一个函数中。我还更新了项删除部分,以使用遍历数组时可以删除的迭代器。然而,这不起作用。。。我真的很感激你的回答,这样我就可以接受了!
 private List<CheckableItem> items = new ArrayList<>();
    // ...
    ButtonComp comp = new ButtonComp();
    comp.init();  
    items.add(comp);
Iterator<CheckbleItem> it = items.iterator();
while (it.hasNext()) {
    CheckableItem next = it.next();
    if (next.isSelected()) {
        mainPanel.remove(next.getComponent());
        it.remove();
        continue;
    }
}