gpt4 book ai didi

java - 维护 JInternalFrame 的单个实例?

转载 作者:行者123 更新时间:2023-12-02 08:36:06 31 4
gpt4 key购买 nike

我有一个打开多个 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);
}
}

最佳答案

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

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

我建议使用 HIDE_ON_CLOSE 作为您的 default close operation在框架上并在关键监听器中使用 setVisible(false) 而不是 dispose()。当框架被处置时,它们将被关闭,并且您不应该在关闭后尝试重新使用框架。如果您只是隐藏框架,它仍然是桌面 Pane 的子级,因此当您在 setInternalFrame 方法中找到框架时,应该添加对 setVisible(true) 的调用.

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

关于java - 维护 JInternalFrame 的单个实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1736809/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com