Set location of JDialog relative to JFrame(设置 JDialog 相对于 JFrame 的位置)
问题描述
有没有办法设置相对于 JFrame 的对话框位置?
Is there a way to set a dialog location relative to a JFrame
?
我想将对话框居中放置在我的 GUI 所在的框架上,而不是对话框经常出现在屏幕中心而不是 GUI 内.
I would like to center the dialog to the frame that houses my GUI, instead the dialog often appears in the center of the screen rather than within the GUI.
推荐答案
有没有办法设置相对于另一个 JFrame 的位置
Is there a way to set a location relative to another JFrame
你可以:
- 传递
JFrame
作为 JDialog.setLocationRelativeTo(组件 c) 从
/Window.html#setLocation%28java.awt.Point%29" rel="noreferrer">JDialog.setLocation(Point p)JFrame
到 Point
- pass
JFrame
as argument for JDialog.setLocationRelativeTo(Component c) - set to desired
Point
fromJFrame
to JDialog.setLocation(Point p)
编辑
所有 Swing 代码都必须在 EventDispatchThread 上完成,意思是setVisible(true)
应该被包装到 invokeLater
all Swing code must be done on EventDispatchThread, meaning that setVisible(true)
should be wrapped into invokeLater
EDIT2
当使用 JFrame 作为组件 c 时,我得到一个找不到符号错误
when using the JFrame as Component c I am getting a cannot find symbol error
不可能,有点小错误
这篇关于设置 JDialog 相对于 JFrame 的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!