[英]Creating JPanels with a titled border in a for loop from user input
我想創建一個for循環,該循環創建帶有標題標題的JPanel
容器。 迭代次數取決於先前界面的用戶輸入。
int noofpara=Integer.parseInt(data[6]);
for(int i=1;i<=noofpara;i++){
jPanel1.add(new JPanel().setBorder(new TitledBorder("Perimeter"+i)));
}
noofpara
是用戶根據for循環應創建帶有標題邊框和邊框數的面板而選擇的邊框數。 該錯誤出現在jpanel1.add...
,顯示不允許使用無效類型。
JPanel#setBorder
方法具有void
返回類型,這意味着調用該方法時它不返回任何值。
但是JPanel#add
方法需要一個值才能調用,因為setBorder為空,它會給出編譯錯誤。
您可以通過此方法簡單地解決此問題。
JPanel childPanel = new JPanel();
childPanel.setBorder(new TitledBorder("Perimeter" + i));
jPanel1.add(childPanel);
您必須制作新面板並添加。
for (int i = 1; i <= noofpara; i++) {
JPanel innerPane = new JPanel();
innerPane.setBorder(new TitledBorder("Perimeter" + i));
jPanel1.add(innerPane);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.