gpt4 book ai didi

java - 删除特定按钮

转载 作者:行者123 更新时间:2023-12-02 10:57:58 24 4
gpt4 key购买 nike

我在 JFrame 中有一个 JPanel,其中包含 5 个按钮。在另一个 JPanel 中有一个名为“删除按钮”的按钮,我想要做的是单击此按钮,然后通过点击其中一个来选择要删除其他 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)
}
}

最佳答案

在按钮监听器中使用责任链。 One 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);
}
}
}

关于java - 删除特定按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9827517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com