从JPanel中删除特定组件 - Java

时间:2015-11-30 08:14:37

标签: java swing jpanel jbutton actionevent

我目前在JFrame中的JPanel(FlipPanel)中有多个JPanel(ItemPanel)。当按下单独面板(FlipFormPanel)中的JButton时,将ItemPanel添加到FlipPanel。我在ItemPanel上有一个JButton标记为"取消"我想删除按下Cancel JButton的特定ItemPanel。我目前只删除了其按钮被选中的ItemPanel,但之后没有其他按钮会删除任何更多的ItemPanel。

如何修复我的代码以完成我想要完成的任务?

我的每个班级的代码如下。我试图尽可能多地去除无关的材料,以避免混乱手头的任务。我相信问题位于方法addItemPanel(setItemListener(下的FlipPanel中,以及ItemPanelListener和ItemPanelEvent类中。感谢您提供的任何帮助。

FlipPanel:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import net.miginfocom.swing.MigLayout;

public class FlipPanel extends JPanel {

private JPanel mainPanel;
private JScrollPane scrollPane;
private ItemPanel itemPanel;
private JPanel titlePanel;
private JLabel titleLabel;

private List<ItemPanel> itmPnls = new ArrayList<ItemPanel>();
private int count;

public FlipPanel() {

    setLayout(new BorderLayout());

    mainPanel = new JPanel();
    scrollPane = new JScrollPane(mainPanel);
    add(scrollPane, BorderLayout.CENTER);

    mainPanel.setLayout(new MigLayout("align center, fillx", "fill", ""));
    titlePanel = new JPanel();
    mainPanel.add(titlePanel, "wrap, gapbottom 13, gaptop 7");
    titlePanel.setLayout(new GridBagLayout());
    titleLabel = new JLabel("CURRENT FLIPS");
    Font labelFont = titleLabel.getFont();
    titleLabel.setFont(new Font(labelFont.getName(), Font.BOLD, 25));
    titlePanel.setBorder(BorderFactory.createMatteBorder(0, 0, 3, 0, Color.BLACK));
    titlePanel.add(titleLabel);

    count = 0;
}

public void addItemPanel(String item, String buyPrice, String sellPrice,
        String quantity, String pcBuyPrice, String pcSellPrice) {
    this.itemPanel = new ItemPanel(count, item, buyPrice, sellPrice, quantity,
            pcBuyPrice, pcSellPrice);

    mainPanel.add(itemPanel, "wrap");

    itmPnls.add(itemPanel);
    count++;

    setItemListener(this.itemPanel);
}

public void setItemListener(ItemPanel iP) {
    iP.setItemPanelListener(new ItemPanelListener() {
        public void itemPanelEventOccurred(ItemPanelEvent e) {;

            System.out.println("Remove");
            System.out.println("Current Position: " + e.getPosition());
            mainPanel.remove(itmPnls.get(e.getPosition()));

            System.out.println("Positions:");
            for (int i = 0; i < count; i++) {
                System.out.println(itmPnls.get(i).getPosition());
            }

            if(e.getPosition() < count - 1) {
                for (int i = e.getPosition() + 1; i < count; i++) {
                    itmPnls.get(i).setPosition(i);
                }
            }

            count--;
            revalidate();
            repaint();

            System.out.println("Positions:");
            for (int i = 0; i < count; i++) {
                System.out.println(itmPnls.get(i).getPosition());
            }

            setItemListener(itmPnls.get(0));
        }

    });
}

ItemPanel:

import javax.swing.JPanel;

public class ItemPanel extends JPanel {

private int position;

private String item;
private String buyPrice;
private String sellPrice;
private String quantity;
private String pcBuyPrice;
private String pcSellPrice;

private JButton logBtn;
private JButton editBtn;
private JButton cancelBtn;



private ItemPanelListener itemPanelListener;

public ItemPanel(int position, String item, String buyPrice, String sellPrice,
        String quantity, String pcBuyPrice, String pcSellPrice) {
    this.position = position;
    this.item = item;
    this.buyPrice = buyPrice;
    this.sellPrice = sellPrice;
    this.quantity = quantity;
    this.pcBuyPrice = pcBuyPrice;
    this.pcSellPrice = pcSellPrice;

    Dimension dim = getPreferredSize();
    dim.height = 100;
    setPreferredSize(dim);

    setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));

    logBtn = new JButton("Log Item");
    editBtn = new JButton("Edit Item");
    cancelBtn = new JButton("Cancel Item");


    setupLabels();
    setupCancelItemButton();

    layoutComponents();


}

public int getPosition() {
    return position;
}

public void setPosition(int pos) {
    position = pos;
}

public void setupCancelItemButton() {
    cancelBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            ItemPanelEvent ev = new ItemPanelEvent(this);

            if (itemPanelListener != null) {
                itemPanelListener.itemPanelEventOccurred(ev);
            }

        }
    });
}

public void setItemPanelListener(ItemPanelListener listener) {
    this.itemPanelListener = listener;
}

ItemPanelListener:

import java.util.EventListener;


public interface ItemPanelListener extends EventListener {

public void itemPanelEventOccurred(ItemPanelEvent e);

}

ItemPanelEvent:

import java.util.EventObject;

public class ItemPanelEvent extends EventObject {

private int position;

public ItemPanelEvent(Object source) {
    super(source);
}

public ItemPanelEvent(Object source, int position) {
    super(source);
    this.position = position;
}

public int getPosition() {
    return position;
}

}

0 个答案:

没有答案