![](/img/trans.png)
[英]How would I use an ActionListener on a Button to change JLabels in a different class?
[英]Would it be a bad idea to use a For loop to create multiple JLabels that will be part of the implementation of an ActionListener later on?
我正在嘗試創建同一JLabel的多個副本。 就像是,
int totalLbls = 6;
JLabel lbl[] = new JLabel[totalLbls];
for (int i = 0; i <totalLbls;i++){
lbl[i] = new JLabel("default");
subPanel.add(lbl[i]);
}
我創建的標簽最終將用於顯示數據。 單擊JButton“發送”時,這些JLabel將顯示相關數據,這些數據將使用外部JAR文件中的方法。
每個JLabel必須顯示自己的唯一數據。 因此,我必須手動創建6個JLabel並將數據手動“映射”到每個JLabel還是可以使用我創建的for循環來完成。
數組實際上是一個很好的主意,數組使處理相當大的一組相同數據變得容易得多,最大的好處是不必命名單個對象。但是如果您希望每個對象都具有一定的大小,則基於所顯示的數據無論如何都將必須在循環外部進行操作,但仍然.....沒有名字可擔心。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.