2014-02-26 50 views
2

我得到一个任务,创建一个可调用多个我的小程序工作(最后一个任务调用SpherePainter)的JInternalFrame,并且可以统计内部运行了多少个“小程序” JinternalFrame如何在JInternalFrame中单击Close(X)按钮时执行某些操作

public class MyInternalFrame extends JInternalFrame { 
static int openFrameCount = 0; 
static final int xOffset = 30, yOffset = 30; 

public MyInternalFrame() { 
    super("SpherePainter #" + (++openFrameCount), 
      true, 
      true, 
      true, 
      true); 

    setSize(500,500); 
    //Set the window's location. 
    setLocation(xOffset*openFrameCount, yOffset*openFrameCount); 
    SpherePainterApplet sp = new SpherePainterApplet(); 
    sp.init(); 
    this.add(sp); 

} 

}

我可以调用没有问题的小程序[小程序在里面另一个JInternalFrame运行],但我不能指望有多少人是“实际”运行。

当一个applet被创建时,我的方法是checkApp++,当一个applet被关闭时,我的方法是checkApp--。 有没有办法使用关闭按钮(X)或类似的监听器?

+0

请查看http://docs.oracle.com/javase/tutorial/uiswing/events/internalframelistener.html中的JInternalFrame.addInternalFrameListener()方法,InternalFrameListener.internalFrameClosing()和InternalFrameListener.internalFrameClosed()侦听器方法和相应的教程。 。 –

+0

它直接到错误404页面,但我会尝试你的代码。谢谢 – user3319146

+0

似乎oracle网站有一些奇怪的重写/重定向检查,请尝试访问http://docs.oracle.com/javase/tutorial/uiswing/TOC.html并选择主题“如何编写内部帧监听器“手动。 –

回答

7

“有没有办法使用关闭按钮(X)或类似的监听器?”

Oleg在评论中指出,似乎适当的答案是使用InternalFrameListener。你可以看到更多在How to write InternalFrameListeners

public class IFrame extends JInternalFrame { 

    public IFrame() { 
     addInternalFrameListener(new InternalFrameAdapter(){ 
      public void internalFrameClosing(InternalFrameEvent e) { 
       // do something 
      } 
     }); 
    } 
} 

侧面说明

“我有一个分配创建一个JInternalFrame时可以调用多我的小应用程序的工作”

谁给了你这个任务,告诉他们读Why CS teachers should stop teaching Java applets甚至可能The Use of Multiple JFrames, Good/Bad Practice? 。国际海事组织似乎是一个无用的无意义的任务,如果要求是你如何描述。

+0

谢谢,我会马上尝试。 – user3319146

+0

谢谢你,这是难以置信的信息。 –

相关问题