任何熟悉java gui - swing軟件包的人......並且知道如何在某種類型的循環中創建像「jlabels和jtextfields」這樣的多個swing對象,這樣您不必分別重複設置每個對象。 ...任何人???以不太冗餘的方式創建多個Jlabel和JTextFields?
例如 - 我要創建60個文本字段和11周的JLabel等 我想(如果可能)不具備單獨創建像這樣
JLabel jlblName = new JLabel("first one");
JLabel jlblName = new JLabel("first two");
JLabel jlblName = new JLabel("first three");
//etc...
每一個可能的話,我想也找到一種方法來設置邊界,並以更短的方式將標籤添加到面板。
我已經嘗試過不同的方法來實現這一點 - 使用數組和創建get方法,仍然沒有運氣。
請幫助我,如果你CAN
下面是我寫的,到目前爲止的例子......然而即使它確實工作,它似乎並不實用的藪原因。 ..我也不知道爲什麼它不起作用。
public class prospectVer2
{
private static int l = 59; //, t = 20, c=11;
public static String getInfo(int b)
{
int a=b;
String [] lah = new String [prospectVer2.l];
lah [0]= "a";
lah [1]= "b";
lah [2]= "c";
lah [3]= "d";
lah [4]= "e";
lah [5]= "f";
lah [6]= "g";
lah [7]= "e";
lah [8]= "f";
lah [9]= "g";
lah [10]= "h";
lah [11]= "i";
lah [12]= "j";
lah [13]= "k";
lah [14]= "l";
lah [15]= "m";
lah [16]= "n";
lah [17]= "o";
lah [18]= "p";
lah [19]= "q";
lah [21]= "r";
lah [22]= "s";
lah [23]= "t";
lah [24]= "u";
lah [25]= "v";
lah [26]= "w";
lah [27]= "x";
lah [28]= "y";
lah [29]= "z";
lah [30]= "aa";
lah [31]= "bb";
lah [32]= "cc ";
lah [33]= " dd";
lah [34]= " ee";
lah [35]= " ff ";
lah [36]= " gg ";
lah [37]= " hh ";
lah [38]= " ii ";
lah [39]= "jj";
lah [40]= "kk";
lah [41]= "ll";
lah [42]= "mm";
lah [43]= "nn";
lah [44]= "oo";
lah [45]= "pp";
lah [46]= "qq";
lah [47]= "rr";
lah [48]= "ss";
lah [49]= "tt";
lah [50]= "uu";
lah [51]= "vv";
lah [52]= "ww";
lah [53]= "xx";
lah [54]= "yy";
lah [55]= "zz";
lah [55]= "aaa";
lah [56]= "bbb";
lah [57]= "ccc";
lah [58]= "ddd";
lah [59]= "eee";
String infos= lah[a];
return infos;
}
public static void main(String[] args)
{
// Declare variables - arrays
// Create Frame and Panel - set size -
JFrame frame = new JFrame("Prospect Assignment");
frame.setSize(700, 900);
JPanel mypanel = new JPanel();
JLabel[] labels = new JLabel[prospectVer2.l];
// Create labels (60) - for loop -
for (int i=0; i<labels.length; i++)
{
labels[i] = new JLabel(prospectVer2.getInfo(i));
mypanel.add(labels[i]);
labels[i].setBounds(i*10+245,i*10+210,120,20);
}
// extra functions for gui
frame.add(mypanel);
mypanel.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
BTW我有使用Java類Swing包 https://cunycitytech.askadmissions.net/emtinterestpage.aspx?ip=prospect 謝謝複製這個網站!如果你試圖啓動您的代碼