[英]Why is it that these JButtons aren't being created?
I used Java graphics to create a 500 x 500 Checkers board within a JPanel, which was placed within a JFrame. 我使用Java图形在JPanel中创建了一个500 x 500 Checkers板,该板放置在JFrame中。 To make the pieces, I wrote a series of for loops to create JButtons across the board.
为了完成这些工作,我编写了一系列for循环,以全面创建JButton。 The following for loops correctly set up the pieces for one side of the board:
以下for循环可正确设置电路板一侧的零件:
for(int x = 355; x>=55;x-=100)
{
Piece p = new Piece();
p.addActionListener(new ButtonListener());
p.setBounds(x,5,40,40);
b.add(p);
}
for(int x = 5;x<=355; x+=100)
{
Piece p = new Piece();
p.addActionListener(new ButtonListener());
p.setBounds(x,55,40,40);
b.add(p);
}
for(int x = 355; x>=55;x-=100)
{
Piece p = new Piece();
p.addActionListener(new ButtonListener());
p.setBounds(x,105,40,40);
b.add(p);
}
However, I just started setting up the pieces for the other side of the board with this for loop, and none of the buttons are being displayed: 但是,我刚刚开始使用此for循环为板的另一侧设置零件,并且没有显示任何按钮:
for(int x = 5; x>=355;x+=100)
{
Piece p = new Piece();
p.addActionListener(new ButtonListener());
p.setBounds(x,255,40,40);
b.add(p);
}
Why is this happening? 为什么会这样呢?
The loop 循环
for (int x = 5; x >= 355; x += 100) {
...
}
won't ever be entered. 永远不会输入。
You are setting x
to 5
. 您将
x
设置为5
。 Then, you are checking if x >= 355
, which will be false
because 5
is not >= 355
. 然后,您正在检查
x >= 355
是否为false
因为5
不是>= 355
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.