今天在学习JAVA JFileChooser类下的showOpenDialog中发现这个Component参数不是太明白,经过查找资料最终解决了这个问题。
无论是showOpenDialog,showSaveDialog还是showDialog函数中都有一个Component parent参数问题。
其实这个问题很简单,简单一句话就是这个参数决定你弹出文件选择器的位置。
下面是JAVA API文档原解释
The parent
argument determines two things: the frame on which the open dialog depends and the component whose position the look and feel should consider when placing the dialog. If the parent is a Frame
object (such as a JFrame
) then the dialog depends on the frame and the look and feel positions the dialog relative to the frame (for example, centered over the frame). If the parent is a component, then the dialog depends on the frame containing the component, and is positioned relative to the component (for example, centered over the component). If the parent is null
, then the dialog depends on no visible window, and it's placed in a look-and-feel-dependent position such as the center of the screen.
翻译后:父参数决定了两件事:打开对话框所依赖的框架和放置对话框时应考虑其外观位置的组件。如果父对象是一个框架对象(例如JFrame),那么该对话框取决于框架和外观,对话框相对于框架的位置(例如,以框架为中心)。如果父元素是一个组件,那么对话框取决于包含该组件的框架,并且相对于该组件定位(例如,以组件为中心)。如果父窗口为空,则对话框依赖于没有可见窗口,并且它被放置在外观和感觉相关的位置,例如屏幕的中心。
parent参数:
null———当前电脑显示器屏幕的中央。
this———当前你编写的程序屏幕中央
如果是你其他的按钮名称就是以这个按钮为中心,弹出的文件选择器。
本人只是一个走在学习JAVA道路上的小菜鸟,哪里有不对的地方,请斧正,谢谢。