Java:Get Root Component

Java:Get Root Component
怎样取得父窗口与最上层的JFrame:
 
java.lang.Object
   |__java.awt.Component
            |__java.awt.Container
                     |__java.awt.Window                                |__javax.swing.JComponent
                              |__java.awt.Frame                                   |__javax.swing.AbstractButton
                                       |__javax.swing.JFrame                               |__javax.swing.JButton
import java.beans.Beans;
1. 最简单的方法
public static Component SwingUtilities.getRoot(Component);
get the root component for the current component tree.(JWindow, JFrame, JApplet...)
 
2. 利用getParent()
Component c = (Component)e.getResource();  // 事件处理中 Object java.util.EventObject.getSource()
while (!Beans.isInstanceOf(c, JFrame.class)) {
        c = c.getParent();
}
if (Beans.isInstanceOf(c, JFrame.class)) {
        c = (JFrame)c;
}

你可能感兴趣的:(Java:Get Root Component)