维护 JInternalFrame 的单个实例?
我有一个打开多个 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);
}
}
I have an application which opens multiple JIFs, But I only want to create a single instance of the JIF, so I use these function to check that, and I use dispose to close the JIF after a key is pressed(JDesktopPane.getSelectedFrame().dispose()). However after 2-3 successive disposes, it doesn't create a new JIF? Am I doing anything wrong over here?
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为
dispose
正在做你想要做的事情。dispose
摆脱框架的操作系统“对等体”。但如果您打算再次显示该框架,那么您不应该丢弃它的基础!我会在 JIF 上使用
setVisible(false)
来隐藏它。然后您可以使用setVisible(true)
重新激活它。I don't think
dispose
is doing what you mean for it to do.dispose
gets rid of the operating system "peer" of your frame. But if you intend to show that frame again, then you shouldn't throw away its underpinnings!I'd go with
setVisible(false)
on the JIF to hide it. Then you can re-activate it withsetVisible(true)
.您正在 for 循环中比较输入参数的类和桌面内部框架。这始终是正确的,因为您的参数是 JInternalFrame 的实例,并且 getAllFrames 方法返回 JInternalFrames 数组。为什么不定期进行比较呢? :
我建议使用
HIDE_ON_CLOSE
作为您的 框架上的默认关闭操作,并在关键侦听器中使用setVisible(false)
而不是dispose()
。当框架被处置时,它们将被关闭,并且您不应该在关闭后尝试重新使用框架。如果您只是隐藏框架,它仍然是桌面窗格的子级,因此当您在setInternalFrame
方法中找到框架时,应该添加对setVisible(true)
的调用。听起来你的行为不一致(你说它在两到三次处置后失败)。这向我表明您遇到了事件线程问题。您的 setInternalFrame 是否在事件线程上被调用?您是否熟悉事件调度线程并且你使用正确吗?
You are comparing the classes of your input parameter and your desktops internal frames in your for loop. This will always be true as your parameter is an instance of JInternalFrame and the getAllFrames method returns an array of JInternalFrames. Why not just do a regular comparison? :
I would recommend using
HIDE_ON_CLOSE
as your default close operation on the frames and usingsetVisible(false)
in your key listener instead ofdispose()
. When frames are disposed they are closed and you should not try and reuse a frame after is has been closed. If you just hide the frame it will still be a child of the desktop pane so you shoud add a call tosetVisible(true)
when you find a frame in yoursetInternalFrame
method.It sounds like you're getting inconsistent behaviour (you say it fails after two or three disposes). This suggests to me that you have an event thread problem. Is your setInternalFrame being called on the event thread? Are you familiar with the Event Dispatch Thread and are you using it correctly?