我們先來看看javax.swing.Popup的javadoc中的描述:
Popups are used to display a Component to the user, typically on top of all the other Components in a particular containment hierarchy. Popups have a very small life cycle. Once you have obtained a Popup, and hidden it (invoked the hide method), you should no longer invoke any methods on it. This allows the PopupFactory to cache Popups for later use.
The general contract is that if you need to change the size of the Component, or location of the Popup, you should obtain a new Popup.
Popup does not descend from Component, rather implementations of Popup are responsible for creating and maintaining their own Components to render the requested Component to the user.
You typically do not explicitly create an instance of Popup, instead obtain one from a PopupFactory.
大致意思就是popup可以把一個組件顯示在視窗的頂層,一般說來它只有很短的生命週期……還有就是建議利用PopupFactory來擷取執行個體。<p>
Popup的一個典型用法就是toolTip你可以從tooltip的源碼中看到。
這裡給出簡單的例子:
Popup mypopup = PopupFactory.getSharedInstance().getPopup(frame,
new JLabel("popupwindow"), posx,posy);
mypopup.show();
這樣就可以只有一個標籤的視窗放在螢幕的任何位置了。
往往可以用,在滑鼠移動時,來顯示一些臨時的資訊等等。