[英]java - selecting all objects in a frame
我有這個:
jLabel1.setBorder(null);
jLabel2.setBorder(null);
jLabel3.setBorder(null);
jLabel4.setBorder(null);
jLabel5.setBorder(null);
jLabel6.setBorder(null);
我想簡化它,減少新手...有什么想法嗎?
嘗試
Component[] components = frame.getContentPane().getComponents();
for (Component component : components) {
if (component instanceof JComponent) {
((JComponent) component).setBorder(null);
}
}
如果只希望JLabel
而不是所有組件都具有空邊框,請更改instanceof
check並將其強制轉換為JLabel
為了包括camickr對您的答案的評論, JLabel
默認情況下沒有邊框,因此您無需執行任何操作。 僅當您在某個時候指定了邊界並希望擺脫邊界時,才應這樣做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.