问题
I have one JFrame
and calling a JDialog
from that frame while initializing. In the dialog have a JButton
, when clicking on the button a new JWindow
opens.
Can I fix the position of window in the main frame? How to access the parent container (JFrame
extended) from JWindow
extended class?
回答1:
Use SwingUtilities.getWindowAncestor, if inside the JWindow
class, refer it as this
, if no, simply put object inside:
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(myJWindow);
来源:https://stackoverflow.com/questions/30122731/how-to-get-the-parent-container-of-a-jwindow-in-swing