[英]Why is it that these JButtons aren't being created?
我使用Java图形在JPanel中创建了一个500 x 500 Checkers板,该板放置在JFrame中。 为了完成这些工作,我编写了一系列for循环,以全面创建JButton。 以下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);
}
但是,我刚刚开始使用此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);
}
为什么会这样呢?
循环
for (int x = 5; x >= 355; x += 100) {
...
}
永远不会输入。
您将x
设置为5
。 然后,您正在检查x >= 355
是否为false
因为5
不是>= 355
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.