gpt4 book ai didi

java - JinternalFrame ActionListener 事件

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:50 25 4
gpt4 key购买 nike

我正在使用 Swing 实现 MVC 模式,我遇到的问题是如何知道事件来自哪个 JinternalFrame,我需要知道这一点,因为我正在关闭被单击的相应窗口,但它不起作用。(我改为关闭其他窗口)

public void actionPerformed(ActionEvent evt) {

String command = evt.getActionCommand();

if (command.equals("cancelar")){
System.out.println("Papitas");
int lastView = views.size();
//views.get(lastView - 1 ).closeView();
//views.get().dispose();
//System.out.println(evt);
}

}

最佳答案

一种方法是获取事件源,然后遍历父级,直到找到包含源的 JInternalFrame。代码看起来有点像这样:

Object current = evt.getSource();
while(current instanceof Component){
if(current instanceof JInteralFrame)
(JInternalFrame current).dispose();
current = ((Component) current).getParent();
}

关于java - JinternalFrame ActionListener 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33383201/

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