兩行代碼讓 JFrame 透明

SetUndecorated(true);
SetBackground(new Color(255, 255, 255, 125);

SetUndecorated 源碼中的註解:

This method can only be called while the frame is not displayable. To make this frame decorated, it must be opaque and have the default shape, otherwise the IllegalComponentStateException will be thrown

即,讓窗口有默認裝飾(decorated),則窗口必須不透明。So,窗口想透明,則必須去掉默認裝飾,即第一句代碼

第二句代碼,new Color 的第 4 個參數代表透明度 a = [0, 255]


註解中還提示, setUndecorated 需要在窗口可視(setVisiable)之前調用,因爲,若讓窗口有默認裝飾,另個一條件是窗口有默認形狀

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章