当我从addMouseListener关闭我的fram时,它说这是不兼容的类型,所以我可以改变它?

问题描述:

我的代码dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING))这么想的工作:当我从addMouseListener关闭我的fram时,它说这是不兼容的类型,所以我可以改变它?

jTable1.addMouseListener(new MouseAdapter() { 

     @Override 
     public void mouseClicked(MouseEvent e) { 

      if (e.getClickCount() == 2) { 
       int select = jTable1.getSelectedRow(); 
       int choi = JOptionPane.showConfirmDialog(null, "Voulez Vous vraiment choisir ce client ?"); 
       if(choi == 0){ 
        Utile.idClient=jTable1.getModel().getValueAt(select, 0).toString(); 
        dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); 

       } 

      } 
     } 
    }); 
+0

请显示您的错误日志。 – Gary99

+0

什么不起作用?你有错误吗?使用调试器并检查代码是否被调用。 –

。要获得外部实例使用:

MyFrame.this 
+0

这个假设类扩展了JFrame,这是不应该的。 – camickr

+0

@HichemJAVA,你的类不应该扩展JFrame,因为你没有改变框架的功能。您应该使用所有组件创建一个JPanel,然后将该面板添加到框架中。在网上搜索“组合vs继承”来理解这两种方法之间的区别。一旦你在设计你的课程时开始使用“组合”,这个解决方案将不起作用。 – camickr

dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); 

“这” 指的MouseListener,而不是框架。

查找活动窗口,你可以这样做:由于您使用的是内部类this点内实例

Window window = SwingUtilities.windowForCompoent(e.getComponent()); 
dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));