[英]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.