单击“删除”按钮后如何删除按钮?

时间:2016-02-27 00:49:57

标签: java swing button

我的程序似乎运行正常,但删除部分除外。每次我点击“删除”按钮,它都会删除自己。所以我的问题是,在点击“删除”按钮后,如何删除选中的按钮? 以下是我的代码片段:

public class DeleteButton extends JFrame implements ActionListener 
{    
  JButton b18a = new JButton("Delete");
  JPanel panel = new JPanel();
  panel.add(b18);

  class ClickListenerTwo implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
         JButton buttonThatWasClicked = (JButton) e.getSource();
         Container parent = buttonThatWasClicked.getParent();
         parent.remove(buttonThatWasClicked);
         parent.revalidate();
         parent.repaint();
    }
  }
}
ActionListener b18aClicked = new ClickListenerTwo();
b18a.addActionListener(b18aClicked); 

P.S - 我正在谈论的这个按钮是在运行时制作的,所以我想在运行时删除它,如果可以的话。谢谢!

2 个答案:

答案 0 :(得分:2)

因此,假设您需要先单击“其他”按钮,您可以使用实例字段来维护对“最后”单击按钮的引用,然后在单击删除按钮时使用该按钮

例如......

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JButton lastButton;

        public TestPane() {
            JPanel grid = new JPanel(new GridLayout(8, 8));
            for (int index = 0; index < 8 * 8; index++) {
                JButton btn = new JButton(Integer.toString(index + 1));
                btn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        lastButton = btn;
                    }
                });
                grid.add(btn);
            }

            JButton delete = new JButton("Delete");
            delete.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (lastButton != null) {
                        lastButton.getParent().remove(lastButton);
                        grid.revalidate();
                        grid.repaint();
                    }
                    lastButton = null;
                }
            });

            setLayout(new BorderLayout());
            add(grid);
            add(delete, BorderLayout.SOUTH);
        }

    }

}

就个人而言,JToggleButton可以提供更好的用户体验

答案 1 :(得分:0)

您必须从顶部框架中找到组件。 可能这段代码会对你有帮助。

public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    if (source instanceof Component) {
        Window w = findWindow((Component) source);
        //Find your component and remove it from this window.
    } else {
        System.out.println("source is not a Component");
    }
}

public static Window findWindow(Component c) {
    System.out.println(c.getClass().getName());
    if (c instanceof Window) {
        return (Window) c;
    } else if (c instanceof JPopupMenu) {
        JPopupMenu pop = (JPopupMenu) c;
        return findWindow(pop.getInvoker());
    } else {
        Container parent = c.getParent();
        return parent == null ? null : findWindow(parent);
    }
}