Java 消除子窗口比消除一系列类型转换更好吗?

Java 消除子窗口比消除一系列类型转换更好吗?,java,swing,window,dismiss,Java,Swing,Window,Dismiss,我有一个子窗口,在数据库上执行搜索。单击子窗口上的“选择”按钮后,我想将搜索结果返回到调用窗口并关闭子窗口。我的代码做得很好,但是,我只是想知道是否有比这更好的方法来关闭子窗口: ((JDialog)((JPanel)(JButton)evt.getSource()).getParent()).getTopLevelAncestor()).dispose(); 获取JPanel父级的中间步骤是不必要的,因为JComponent定义了gettolevelancer(): 为了提高可读性,您还可以

我有一个子窗口,在数据库上执行搜索。单击子窗口上的“选择”按钮后,我想将搜索结果返回到调用窗口并关闭子窗口。我的代码做得很好,但是,我只是想知道是否有比这更好的方法来关闭子窗口:

((JDialog)((JPanel)(JButton)evt.getSource()).getParent()).getTopLevelAncestor()).dispose();

获取
JPanel
父级的中间步骤是不必要的,因为
JComponent
定义了
gettolevelancer()

为了提高可读性,您还可以编写:

SwingUtilities.getWindowAncestor((JButton)event.getSource()).dispose();

对我有用。非常感谢。
SwingUtilities.getWindowAncestor((JButton)event.getSource()).dispose();