English 中文(简体)
Java For Loop Inside Constructor is 无限延期
原标题:Java For Loop Inside Constructor is Infinitely Repeating

So, for some reason when I try to use a for loop to initialize panels in chess board, it actually loops the loop itself. In other words, it doesn t go on forever, but it starts and completes again and again.

package chessgame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChessGame extends JFrame implements ActionListener{

    public static final int WIDTH=800;
    public static final int HEIGHT=800;

    public static void main(String[] args) {
        ChessGame gui = new ChessGame();
        gui.setVisible(true);
    }
    public ChessGame(){
        super("Chess Game Demo");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(8,8));
        JPanel[] chessSquares = new JPanel[64];
        for (int a=0;a<64;a++){
            System.out.println(a);
        }
    }
}

我列入了所有可能相关的法典,因为我计划将ches块的指数用于黑白地。 当我这样做时,我也看到了Null PointerException。 我可以理解,鉴于以下错误,我为什么会忘记这一点,但我根本无法理解为什么要再印刷0、1.62、63份。 我是新来的,对此我绝对没有任何想法。 如果任何人能够解释这一点,将非常有益。 感谢。

最佳回答

Don t put meaningful initialization in ChessGame s constructor, but instead override frameInit. When you do, also be sure to call super.frameInit(). See the javadoc or this tutorial.

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签