完整代码如下:import javax.swing.*;import mdi.MDIFrame.InnerFrame;import java.awt.event.*;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;class MDITest extends JFrame implements ActionListener
{ JDesktopPane desktopPane;
JPanel p;
JButton b1;
JButton b2;
int count = 1; public MDITest()
{
super("MDITest Frame");
Container contentPane = this.getContentPane();
contentPane.setLayout(new BorderLayout()); /*
 * 建立一个新的JDesktopPane并加入于contentPane中
 */
desktopPane = new JDesktopPane();
contentPane.add(desktopPane);
p = new JPanel(new FlowLayout());
contentPane.add(p, BorderLayout.SOUTH); b1 = new JButton("Create New Internal Frames");
b1.addActionListener(this);// 当用户按下按钮时,将运行actionPerformed()中的程序
p.add(b1); b2 = new JButton("Close Selected Frame");
b2.addActionListener(this);//
p.add(b2); setSize(500, 400); addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}); this.setVisible(true);
} public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b1)
{
/*
 * 产生一个可关闭、可改变大小、具有标题、可最大化与最小化的Internal Frame.
 */
JInternalFrame internalFrame = new JInternalFrame("Internal Frame " + (count++), true, true, true, true); internalFrame.setLocation(20, 20);
internalFrame.setSize(300, 300);
internalFrame.setVisible(true);
// 将JInternalFrame加入JDesktopPane中
desktopPane.add(internalFrame); try
{
internalFrame.setSelected(true);
}
catch (java.beans.PropertyVetoException ex)
{
System.out.println("Exception while selecting");
} // 向侦听器列表添加一个 VetoableChangeListener。为所有属性注册该侦听器
internalFrame.addVetoableChangeListener(new VetoableChangeListener()
{ public void vetoableChange(PropertyChangeEvent e) throws PropertyVetoException
{
JInternalFrame frame = (JInternalFrame) e.getSource();
String name = e.getPropertyName();
Object value = e.getNewValue();
if (name.equals("closed") && value.equals(Boolean.TRUE))// 窗口被关闭
{
System.out.println("窗口被关闭");
}
}
});
}
else if (e.getSource() == b2)
{
// 返回此 JDesktopPane 中当前活动的JInternalFrame
JInternalFrame iframe = desktopPane.getSelectedFrame();
if (iframe != null)
{
iframe.dispose();
}
}
} public static void main(String[] args)
{
new MDITest();
}
}
如果直接使用InternalFrame右上角自带的“关闭”按钮关闭该InternalFrame的话,每次都可以监听到该InternalFrame被关闭的事件,即"窗口被关闭"每次都可以正常被打印输出。而当使用外部按钮(代码中的b2按钮)关闭一个JInternalFrame时,监听该Frame关闭的Listener无效,没有任何打印输出!请问该如何解决这个问题?

解决方案 »

  1.   

    监听Frame 关闭的Listener 根本没有加入任务打印Action吧.
    窗口被关闭 是在internalFrame.addVetoableChangeListener(new VetoableChangeListener()......) 被打印出来的.
    b2 没有加入关闭打印动作
      

  2.   

    哦. 要想有打印的话 加个打印Action 在e.getSource 里 应该就可以了吧
      

  3.   

    不好意思,我说的打印,是指在控制台输出 System.out.println("窗口被关闭"); 
    我使用的是VetoableChangeListener来监听窗口是否被关闭了。其实我也试过用InternalFrameListener来监听,效果一样
    上面那段代码是完整的代码,可以直接运行。大虾不妨拷贝到eclipse或者其他ide里运行一下看看。
      

  4.   

    自己搞定,收工把iframe.dispose()改成这样就OK了。
    try
    {
    iframe.setClosed(true);// 关闭此内部窗体
    }
    catch (PropertyVetoException e1)
    {
    e1.printStackTrace();
    }
      

  5.   

    if (listener != NULL)  void Camera::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)sp<CameraListener> listener;
      {
      Mutex::Autolock _l(mLock);
      listener = mListener;
      }
      if (listener != NULL) {
      
      listener->postDataTimestamp(timestamp, msgType, dataPtr);
      } else {
      LOGW("No listener was set. Drop a recording frame.");
      releaseRecordingFrame(dataPtr);
      }
      

  6.   

    #00  pc 00012530  /system/lib/libcamera_client.so (_ZN7android6Camera21dataCallbackTimestampExiRKNS_2spINS_7IMemoryEEE)
    I/DEBUG   (  821):          #01  pc 00008038  /system/lib/libcameraservice.so (_ZN7android13CameraService6Client26handleGenericDataTimestampExiRKNS_2spINS_7IMemoryEEE)
    I/DEBUG   (  821):          #02  pc 0000ab04  /system/lib/libcameraservice.so (_ZN7android13CameraService6Client21dataCallbackTimestampExiRKNS_2spINS_7IMemoryEEEPv)
    I/DEBUG   (  821):          #03  pc 00007eba  /system/lib/libcameraservice.so
    I/DEBUG   (  821):          #04  pc 00005a42  /system/lib/hw/camera.godbox.so (_ZN7android20CameraHardwareGodbox13previewThreadEv)