Gom*_*thi 2 java variables swing
我有三个文本框ct1,ct2,ct3.我必须使用for循环1到3并检查文本框是否为空.那么,在for循环中,我该如何表示它?例如,
for(i=0;i<=3;i++)
{
if(ct+i.getText()) // I know I'm wrong
{
}
}
Run Code Online (Sandbox Code Playgroud)
我有三个文本框ct1,ct2,ct3.
这是你的问题.而不是使用三个单独的变量,创建一个数组或集合:
TextBox[] textBoxes = new TextBox[3];
// Populate the array...
Run Code Online (Sandbox Code Playgroud)
要么:
List<TextBox> textBoxes = new ArrayList<TextBox>();
// Populate the list...
Run Code Online (Sandbox Code Playgroud)
然后在你的循环中:
// Note the < here - not <=
for (int i = 0; i < 3; i++) {
// If you're using the array
String text = textBoxes[i].getText();
// or for the list...
String text = textBoxes.get(i).getText();
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您不需要索引:
for (TextBox textBox : textBoxes) {
String text = textBox.getText();
...
}
Run Code Online (Sandbox Code Playgroud)