[英]Trouble with Java for-loop logic
I am having trouble understanding why my for loop isn't behaving as I would like it. 我无法理解为什么我的for循环无法正常运行。 The purpose for my loop is to add multiple textfields onto a GUI, 70 to be exact.
我的循环的目的是在GUI上添加多个文本字段,准确地说是70。 7 across, 10 down.
7横10倒。 It adds the fields fine, but stops short one row and one column than I want.
它可以很好地添加字段,但是比我想要的短一行又一列。 This seems to be enough information to identify the problem, but I can't, so I came here.
这似乎足以识别问题,但是我不能,所以我来了。
for(int i = 0; i < 6; i++){
for(int j = 0; j < 9; j++){
OT2Field[i][j] = new JTextField();
OT1Field[i][j] = new JTextField();
STField[i][j] = new JTextField();
}
}
int xPointer = 3;
int yPointer = 7;
for(int i = 0; i < 6; i++){
for(int j = 0; j < 9; j++){
addTimeFieldBorder0010(option3, OT2Field[i][j], gridbag, gbc, xPointer, yPointer, 1, 1, 0);
yPointer = yPointer + 3;
}
xPointer++;
yPointer = 7;
}
}
private void addTimeFieldBorder0010(JComponent container, JComponent component,
GridBagLayout gridbag, GridBagConstraints gbc,
int x, int y, int height, int width, double weightx) {
gbc.gridx = x;
gbc.gridy = y;
gbc.gridheight = height;
gbc.gridwidth = width;
gbc.weightx = weightx;
((JTextField) component).setHorizontalAlignment(JLabel.CENTER);
component.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.red));
component.setFont(component.getFont().deriveFont(18.0f));
component.setForeground(Color.red);
component.setBackground(Color.YELLOW);
gridbag.setConstraints(component, gbc);
container.add(component, gbc);
}
According to the Java Language Specification §15.20.1 , 根据Java语言规范§15.20.1 ,
- The value produced by the
<
operator istrue
if the value of the left-hand operand is less than the value of the right-hand operand, and otherwise isfalse
.如果左侧操作数的值小于右侧操作数的值,则由
<
运算符产生的值为true
,否则为false
。
So you are starting at i = 0
and looping while i
is less than 6. You need to loop while it is less than 7, or less than or equal to 6. The same applies to your next loop. 因此,您从
i = 0
并在i
小于 6时循环。在小于7或小于或等于6时需要循环。下一个循环也是如此。
Change your two loops to: 将您的两个循环更改为:
for(int i = 0; i < 7; i++){
for(int j = 0; j < 10; j++){
//stuff
}
}
Your outer loop only executes from 0 to 5 and inner loop only executes from 0 to 8. Change the loop to 您的外部循环仅从0到5执行,内部循环仅从0到8执行。将循环更改为
for(int i = 0; i < 7; i++){
for(int j = 0; j < 10; j++){
OT2Field[i][j] = new JTextField();
OT1Field[i][j] = new JTextField();
STField[i][j] = new JTextField();
}
}
The <
symbol return false when the value on the left is equal to right. 当左侧的值等于右侧时,
<
符号返回false。 So for i=6
, i<6
returns false and hence you are missing one iteration. 因此,对于
i=6
, i<6
返回false,因此您缺少一次迭代。
You're cycling between 0 to 5
for loop i
and from 0 to 8
for loop j
. 对于循环
i
,您正在0 to 5
之间循环,对于循环j
从0 to 8
循环。 That's why it stops short one row and one column. 这就是为什么它停止短一行和一列。 You should change them as follow:
您应该按如下方式更改它们:
for(int i = 0; i <= 6; i++){
for(int j = 0; j <= 9; j++){
...
}
}
or 要么
for(int i = 0; i < 7; i++){
for(int j = 0; j < 10; j++){
...
}
}
for(int i = 0; i <= 6; i++){
for(int j = 0; j <= 9; j++)
Your loop should be 你的循环应该是
for(int i = 0; i < 7; i++)
{
for(int j = 0; j < 10; j++)
{
//your code
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.