Java 如何使用另一个类的actionPerformed方法删除一个类/组件中的按钮?
在我正在设计的代码中,我在应用程序的主JFrame中实例化了一个JButton,它使用一个ActionListener引用另一个类/组件中的actionPerformed方法。我想知道是否有一种方法可以在运行上述actionPerformed方法(单击它)时删除该按钮。换句话说,actionPerformed方法是否有可能引用回激活它的按钮?能否获取导致ActionListener激活的对象的引用?是,通过ActionEvent参数的Java 如何使用另一个类的actionPerformed方法删除一个类/组件中的按钮?,java,swing,Java,Swing,在我正在设计的代码中,我在应用程序的主JFrame中实例化了一个JButton,它使用一个ActionListener引用另一个类/组件中的actionPerformed方法。我想知道是否有一种方法可以在运行上述actionPerformed方法(单击它)时删除该按钮。换句话说,actionPerformed方法是否有可能引用回激活它的按钮?能否获取导致ActionListener激活的对象的引用?是,通过ActionEvent参数的getSource()方法 i、 e 不过有一个警告:如果您在
getSource()
方法
i、 e
不过有一个警告:如果您在多个设置中使用此ActionListener或Action,例如与菜单一起使用,则源可能根本不是按钮
至于使用此按钮“删除”按钮,则需要获取组件的容器,如:
public void actionPerformed(ActionEvent evt) {
JButton button = (JButton) evt.getSource(); // danger when casting!
Container parent = button.getParent();
parent.remove(button);
parent.revalidate();
parent.repaint();
}
但是,我必须再次警告,如果事件不是由JButton引起的,或者如果父级使用的布局管理器没有删除对象,那么您将遇到麻烦。我想知道在这里使用卡片布局是否更好
e、 g
导入java.awt.Container;
导入java.awt.event.ActionEvent;
导入javax.swing.*;
公共类RemoveButton扩展了JPanel{
私有静态最终长serialVersionUID=1L;
私有静态最终整数按钮计数=5;
私人行动removeMeAction=新的removeMeAction(“删除我”);
公共移除按钮(){
对于(int i=0;i<按钮计数;i++){
添加(新JButton(removeMeAction));
}
}
私有静态void createAndShowGui(){
RemoveButton主面板=新建RemoveButton();
JFrame=新JFrame(“移除按钮”);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(主面板);
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
createAndShowGui();
}
});
}
}
类RemoveMeAction扩展了AbstractAction{
私有静态最终长serialVersionUID=1L;
public RemoveMeAction(字符串名称){
super(name);//设置按钮的文本和操作命令
}
@凌驾
已执行的公共无效操作(操作事件evt){
Object source=evt.getSource();
//AbstractButton是JButton和其他类的父类
if(抽象按钮的源实例){
AbstractButton=(AbstractButton)源;
容器父级=button.getParent();
移除(按钮);
parent.revalidate();
parent.repaint();
}
}
}
谢谢您的快速回复,但您能否更详细地告诉我如何实际使用该方法?具体来说,如何使用它来删除源代码?当我这样做,然后尝试执行删除(theSource)方法时,它不起作用。我得到红线,显然是因为源是对象的实例,而不是组件。成功!非常感谢,也感谢你的警告。幸运的是,这是我正在编写的一个非常简单的程序(我必须为我的1410类设计一个Tictatoe游戏),因此除了JButton之外,没有其他东西不会调用它的实例(主要是因为JButton是唯一被使用的东西)。
public void actionPerformed(ActionEvent evt) {
JButton button = (JButton) evt.getSource(); // danger when casting!
Container parent = button.getParent();
parent.remove(button);
parent.revalidate();
parent.repaint();
}
import java.awt.Container;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class RemoveButton extends JPanel {
private static final long serialVersionUID = 1L;
private static final int BUTTON_COUNT = 5;
private Action removeMeAction = new RemoveMeAction("Remove Me");
public RemoveButton() {
for (int i = 0; i < BUTTON_COUNT; i++) {
add(new JButton(removeMeAction));
}
}
private static void createAndShowGui() {
RemoveButton mainPanel = new RemoveButton();
JFrame frame = new JFrame("Remove Buttons");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class RemoveMeAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public RemoveMeAction(String name) {
super(name); // to set button's text and actionCommand
}
@Override
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
// AbstractButton is the parent class of JButton and others
if (source instanceof AbstractButton) {
AbstractButton button = (AbstractButton) source;
Container parent = button.getParent();
parent.remove(button);
parent.revalidate();
parent.repaint();
}
}
}