【问题标题】:How to do something when Close (X) button is clicked in JInternalFrame在 JInternalFrame 中单击关闭 (X) 按钮时如何做某事
【发布时间】:2014-03-29 12:52:09
【问题描述】:

我有一个任务要创建一个JInternalFrame,它可以调用我的多个小程序工作(最后一个任务调用SpherePainter),并且可以计算JinternalFrame 中运行了多少“小程序”。

[小程序在另一个JInternalFrame中运行]

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);

}

}

我可以毫无问题地调用小程序,但我无法计算其中有多少是“实际”运行的。

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

【问题讨论】:

  • 检查 JInternalFrame.addInternalFrameListener() 方法、InternalFrameListener.internalFrameClosing() 和 InternalFrameListener.internalFrameClosed() 侦听器方法以及docs.oracle.com/javase/tutorial/uiswing/events/…的相应教程。
  • 它直接到错误 404 页面,但我会尝试你的代码。谢谢
  • 好像 oracle 站点有一些奇怪的重写/重定向检查,请尝试转到 docs.oracle.com/javase/tutorial/uiswing/TOC.html 并手动选择主题“如何编写内部帧侦听器”。

标签: java swing applet jinternalframe


【解决方案1】:

“有没有办法使用关闭按钮(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?。如果要求如您所描述的那样,IMO 似乎是一个无用的废话任务。

【讨论】:

  • 谢谢你,这是很难得到的信息。
猜你喜欢
  • 2022-08-05
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多