Java: how to remove specific object when button clicked

故事扮演 提交于 2019-12-10 12:18:29

问题


I'm trying to remove an object by clicking it's button, but what happens is when I click the button inside the panel it removes the last created panel.

Question is how will I remove the specific panel that I want?

Here's my code:

public class TimerPractice extends JFrame implements ActionListener
{
    JPanel main=new JPanel();
    JPanel gui=new JPanel();
    JButton btnadd=new JButton("Add Timer");
    JPanel order=new JPanel();

    public TimerPractice()
    {

        main.setLayout(new BorderLayout());
        gui.setLayout(new FlowLayout());
        main.add(btnadd, BorderLayout.NORTH);
        main.add(gui,BorderLayout.CENTER);
        add(main);
        btnadd.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                //addPanel();
                //System.out.print(x);
                addPanel();
                revalidate();
                repaint();
            }
        });

        main.add(gui);
    }


    public void addPanel()
    {
        Border blackline=BorderFactory.createLineBorder(Color.BLACK);
        order=new JPanel();
        order.setPreferredSize(new Dimension(200,300));
        order.setLayout(new BorderLayout());
        TitledBorder title=BorderFactory.createTitledBorder(blackline);
        title.setTitleJustification(TitledBorder.LEFT);
        order.setBorder(title);
        addBtn();
        gui.add(order);
    }

    public void addBtn()
    {
        JButton remove=new JButton("Remove");
        order.add(remove, BorderLayout.SOUTH);
        remove.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                gui.remove(order);
                revalidate();
                repaint();
            }
        });
    }

    public static void main(String args[])
    {
        TimerPractice p=new TimerPractice();
        p.setSize(1000,800);
        p.setVisible(true);
        p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p.setLocationRelativeTo(null);
    }
}

回答1:


Each time you add a new order panel, you are reassigning the reference to order to point to the last created JPanel, this means that when you say remove(order), it only knows how to remove the last panel you created...

Long answer, separate each order panel into it's own, self contained and managed entity, this way you won't run into the reference issues you are having right now...

Short answer, extract the reference from the ActionEvent...

public void actionPerformed(ActionEvent e)
{
    Object source = e.getSource();
    if (source instanceof Component) {
        Component comp = (Component)source;
        gui.remove(comp.getParent());
        revalidate();
        repaint();
    }
}

You could use an Action to generate a self contained unit of work, which you would pass a reference of the current instance of order, this would allow the Action to maintain it's own reference separate from the TimerPractice class



来源:https://stackoverflow.com/questions/25028396/java-how-to-remove-specific-object-when-button-clicked

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!