删除特定按钮

时间:2012-03-22 17:34:06

标签: java swing jpanel jbutton actionlistener

我在JFrame中有一个包含5个按钮的JPanel。在另一个JPanel中有一个名为“删除按钮”的按钮,我想要做的是单击此按钮,然后通过ckicking其中一个来选择要删除的其他5个按钮。任何人都可以帮助我吗?

    public class gui extends JFrame implements ActionListener
    {
      JPanel p1 = new JPanel();
      JPanel p2 = new JPanel();
      JPanel p2 = new JPanel();

      JButton b1 = new JButton("Delete");
      JButton b2 = new JButton("A");
      JButton b3 = new JButton("B");
      JButton b4 = new JButton("C");

      gui()
      {
        p1.setLayout(new GridLayout(1,2));
        p1.add(p2);
        p1.add(p3);

        p2.setLayout(new GridLayout(3,1));
        p2.add(b2);
        p2.add(b3);
        p2.add(b4);
        p3.add(b1);

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);

      }

     public void actionPerformed(ActionEvent e)
     {
       if (e.getSource() == b1)
         // When I click this button I want to be able to delete a button of my choice (one of the other 3)
     }
   }

5 个答案:

答案 0 :(得分:5)

在按钮侦听器中使用责任链。一个Button监听器,用于监听“待删除”按钮和“删除”按钮。在正常操作下,此按钮侦听器只是将“待删除”按钮事件发送到现有按钮事件,但是当它听到“删除”按钮事件时,它会捕获“下一个”按钮事件而不将其发送到现有按钮侦听器,并采取行动删除按钮。

好的,你提供了一些代码。这是一个使用责任链的解决方案。基本上,如果一个ActionListener无法处理该事件,它会将其发送到下一个事件,依此类推。

import java.awt.GridLayou;
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.WindowConstants;

public class Gui extends JFrame {

  public static final long serialVersionUID = 1L;

  JPanel p1 = new JPanel();
  JPanel p2 = new JPanel();
  JPanel p3 = new JPanel();

  JButton b1 = new JButton("Delete");
  JButton b2 = new JButton("A");
  JButton b3 = new JButton("B");
  JButton b4 = new JButton("C");

  public Gui() {

    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    p1.setLayout(new GridLayout(1, 2));
    p1.add(p2);
    p2.add(p3);

    p2.setLayout(new GridLayout(3, 1));
    p2.add(b2);
    p2.add(b3);
    p2.add(b4);

    p3.add(b1);

    DoItListener doIt = new DoItListener(null);
    DeleteItListener deleteIt = new DeleteItListener(this, doIt);

    b1.addActionListener(deleteIt);
    b2.addActionListener(deleteIt);
    b3.addActionListener(deleteIt);
    b4.addActionListener(deleteIt);

    add(p1);
    pack();
  }

  public void deleteButton(String name) {
    if (b2 != null && "A".equals(name)) {
      p2.remove(b2);
      b2 = null;
      p2.invalidate();
      p2.redraw();
    }
    if (b3 != null && "B".equals(name)) {
      p2.remove(b3);
      b3 = null;
      p2.invalidate();
      p2.redraw();
    }
    if (b4 != null && "A".equals(name)) {
      p2.remove(b4);
      b4 = null;
      p2.invalidate();
      p2.redraw();
    }
  }

  public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new Gui().setVisible(true);
      }
    });
  }
}

class DoItListener implements ActionListener {

  private ActionListener delegate;

  public DoItListener(ActionListener next) {
    delegate = next;
  }

  public void actionPerformed(ActionEvent e) {
    if (!("Delete".equals(e.getActionCommand()))) {
      System.out.println("doing " + e.getActionCommand());
    } else if (delegate != null) {
      delegate.actionPerformed(e);
    }
  }
}

class DeleteItListener implements ActionListener {

  private Gui gui;

  private boolean deleteNext;

  private ActionListener delegate;

  public DeleteItListener(Gui container, ActionListener next) {
    gui = container;
    delegate = next;
    deleteNext = false;
  }

  public void actionPerformed(ActionEvent e) {
    if ("Delete".equals(e.getActionCommand())) {
      deleteNext = true;
    } else if (deleteNext) {
      gui.deleteButton(e.getActionCommand());
      deleteNext = false;
    } else if (delegate != null) {
      delegate.actionPerformed(e);
    }
  }
}

答案 1 :(得分:2)

这是一段代码,可以帮助您朝着正确的方向前进:

import java.awt.event.ActionEvent;
import javax.swing.*;

public class FrameTestBase extends JFrame {

    public static void main(String args[]) {
        FrameTestBase t = new FrameTestBase();

        final JPanel p = new JPanel();

        final JButton button = new JButton();

        button.setAction(new AbstractAction("Remove me!") {
            @Override
            public void actionPerformed(ActionEvent e) {
                p.remove(button);
                p.revalidate();
                p.repaint();
            }
        });

        p.add(button);
        t.setContentPane(p);

        t.setDefaultCloseOperation(EXIT_ON_CLOSE);
        t.setSize(400, 400);
        t.setVisible(true);
    }
}

点击之前:

enter image description here

点击后:

enter image description here


来自评论:

为了概括这一点,您可以创建一个AbstractAction,将待删除按钮作为参数。使用此AbstractAction,并在需要更改删除策略时根据需要进行更新。

答案 2 :(得分:2)

在这里试试这段代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DeleteButtonExample extends JFrame
{
    private boolean deleteNow = false;
    private JButton deleteButton;
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JButton[] buttons = new JButton[5];

    private ActionListener deleteAction = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            JButton button = (JButton) ae.getSource();
            if (deleteNow)
            {
                leftPanel.remove(button);
                leftPanel.revalidate();
                leftPanel.repaint();
                deleteNow = false;
            }
            else
            {
                // Do your normal Event Handling here.
                System.out.println("My COMMAND IS : " + button.getActionCommand());
            }
        }
    };

    private void createAndDisplayGUI()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);
        setLayout(new GridLayout(0, 2));

        leftPanel = new JPanel();
        leftPanel.setLayout(new GridLayout(0, 2));
        leftPanel.setBackground(Color.WHITE);
        for (int i = 0; i < 5; i++)
        {
            buttons[i] = new JButton("" + i);
            buttons[i].addActionListener(deleteAction);
            buttons[i].setActionCommand("" + i);
            leftPanel.add(buttons[i]);
        }

        rightPanel = new JPanel();
        rightPanel.setBackground(Color.BLUE);

        JButton deleteButton = new JButton("DELETE");
        deleteButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                JOptionPane.showMessageDialog(null, "Delete any Button from the Left Panel by clicking it."
                                                                                    , "INFO : ", JOptionPane.INFORMATION_MESSAGE);
                deleteNow = true;                                                                   
            }
        });
        rightPanel.add(deleteButton);

        add(leftPanel);
        add(rightPanel);
        pack();
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new DeleteButtonExample().createAndDisplayGUI();
            }
        });
    }
}

输出:

FIRST WINDOWWHEN YOU CLICKED DELETE BUTTONAFTER YOU CLICK THE BUTTON ON LEFT PANEL IT WILL BE DELETED AS SHOWN HERE

答案 3 :(得分:1)

看一下玻璃窗格。这个tutorial显示了它的使用方式。

在较高级别,单击“删除”按钮会使玻璃窗格侦听器进入以下状态:

  1. 检测到点击,
  2. 确定目标组件
  3. 确定是否允许删除该组件
  4. 如果是,请删除该组件。
  5. 作为设计说明,我会保留允许删除的Set个控件,从而将问题分开。因此,当您添加允许删除的按钮时,您还有责任将其添加到删除候选集中。

答案 4 :(得分:0)

最简单的方法:

  1. 向按钮添加一个 ActionListener 以删除另一个按钮;
  2. 重新绘制并重新验证要移除按钮所在的面板。

示例(在这种情况下,将删除另一个面板的按钮由“deleteBtn”调用,另一个面板中将被删除的按钮由“btnToDlt”调用>”存在于“面板”中):

deleteBtn.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            panel.remove(btnToDlt);
            panel.revalidate();
            panel.repaint();
            
        }
            
            });