0

我有一个打开多个 JIF 的应用程序,但我只想创建一个 JIF 实例,所以我使用这些函数来检查它,并在按下一个键后使用 dispose 关闭 JIF(JDesktopPane.getSelectedFrame() .dispose())。但是连续2-3次处理后,它不会创建一个新的JIF吗?我在这里做错什么了吗?

public static void setInternalFrame(final JInternalFrame internalFrame) {
    log.debug("CurActiveInternalFrame " + ShoppyPOSApp.getCurrentActiveInternalFrame(), null);
    log.debug("Incoming internalFrame " + internalFrame, null);

    boolean isFrameFound = false;
    try {
        // Have a check whether the DesktopPane contains the internal Frame
        // If yes bring it to front and set the focus
        for (int i = 0; i < ShoppyPOSApp.frame.mainDesktopPane.getAllFrames().length; i++) {
            if (ShoppyPOSApp.frame.mainDesktopPane.getAllFrames()[i].getClass() == internalFrame.getClass()) {
                isFrameFound = true;
            }
        }

        if (!isFrameFound) {
            internalFrame.setVisible(true);
            internalFrame.setLocation(
                ShoppyPOSApp.frame.mainDesktopPane.getWidth()/ 2 - internalFrame.getWidth() / 2,
                ShoppyPOSApp.frame.mainDesktopPane.getHeight() / 2 - internalFrame.getHeight() / 2
            );
            ShoppyPOSApp.frame.mainDesktopPane.add(internalFrame);
        }
        internalFrame.setSelected(true);
    } catch (Exception e) {
        log.debug(e.toString(), null);
    }
}
4

2 回答 2

1

我不认为dispose是按照你的意思去做。dispose摆脱框架的操作系统“对等”。但是,如果您打算再次展示该框架,那么您不应该丢弃它的基础!

我会setVisible(false)在 JIF 上隐藏它。然后您可以使用 重新激活它setVisible(true)

于 2009-11-15T06:48:50.877 回答
1

您正在比较您的输入参数的类和您的 for 循环中的桌面内部框架。这总是正确的,因为您的参数是 JInternalFrame 的一个实例,而 getAllFrames 方法返回一个 JInternalFrames 数组。为什么不定期进行比较?:

ShoppyPOSApp.frame.mainDesktopPane.getAllFrames()[i] == internalFrame

我建议HIDE_ON_CLOSE您在框架上使用默认关闭操作setVisible(false),并在您的关键侦听器中使用而不是dispose(). 处理框架时,它们会关闭,您不应尝试在框架关闭后重新使用框架。如果您只是隐藏框架,它仍然是桌面窗格的子项,因此您应该setVisible(true)在方法中找到框架时添加调用setInternalFrame

听起来您的行为不一致(您说它在两次或三次处置后失败)。这向我表明您有一个事件线程问题。您的 setInternalFrame 是否在事件线程上被调用?您是否熟悉Event Dispatch Thread并正确使用它?

于 2009-11-17T22:10:12.273 回答