使用数组创建多个textField

时间:2017-12-30 05:06:20

标签: java swing

我试图创建一个全部在数组中的9x9 textFields网格。左上角称为字段[1] [1],右下角字段为[9] [9]。我到目前为止编写的代码是

    int NUM_FIELDS_X = 1; //x textField
    int NUM_FIELDS_Y = 1; // y textField
    int x = 21; // x location of textField
    int y = 21; // y location of textField

    TextField[][] fields = new TextField[NUM_FIELDS_X][NUM_FIELDS_Y]; {
        for (NUM_FIELDS_Y =1; NUM_FIELDS_Y <= 9; NUM_FIELDS_Y++) {

            for (NUM_FIELDS_X =1; NUM_FIELDS_X <= 9; NUM_FIELDS_X++) {

                    fields[NUM_FIELDS_X][NUM_FIELDS_Y] = new TextField();

                    fields[NUM_FIELDS_X][NUM_FIELDS_Y].setColumns(10);
                    fields[NUM_FIELDS_X][NUM_FIELDS_Y].setBounds(x, y, 32, 32);
                    frame.getContentPane().add(fields[NUM_FIELDS_X][NUM_FIELDS_Y]);

                    x=x+32;
                }
            y = y+32;
        }
    }

但是,一旦启动它我就会收到错误,并且for循环无法启动。我收到的错误信息是

java.lang.ArrayIndexOutOfBoundsException: 1
at sudokuSolver.sudokuInterface.initialize(sudokuInterface.java:70)
at sudokuSolver.sudokuInterface.<init>(sudokuInterface.java:44)
at sudokuSolver.sudokuInterface$1.run(sudokuInterface.java:31)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

感谢Akash Jha和coder-croc的帮助,我设法解决了这个问题。我已将代码更改为

    int NUM_FIELDS_X = 0; //x number of textField
    int NUM_FIELDS_Y = 0; // y number of textField
    int x = 21; // x location of textField
    int y=21; // y location of textField



    TextField[][] fields = new TextField[9][9]; {
        for (NUM_FIELDS_Y =0; NUM_FIELDS_Y < 9; NUM_FIELDS_Y++) {
            x=21;
            for (NUM_FIELDS_X =0; NUM_FIELDS_X <= 8; NUM_FIELDS_X++) {

                    fields[NUM_FIELDS_X][NUM_FIELDS_Y] = new TextField();

                    fields[NUM_FIELDS_X][NUM_FIELDS_Y].setColumns(10);
                    fields[NUM_FIELDS_X][NUM_FIELDS_Y].setBounds(x, y, 32, 32);
                    frame.getContentPane().add(fields[NUM_FIELDS_X][NUM_FIELDS_Y]);

                    x=x+32;
                }
            y = y+32;
        }
    }

2 个答案:

答案 0 :(得分:1)

有问题的代码,

NUM_FIELDS_Y = 1; NUM_FIELDS_Y <= 9;

您的数组从fields[0][0]fields[8][8]的长度为9(索引0到8 ),NUM_FIELDS_Y <= 9条件将会访问fields[9][9]因此你得到的是ArrayIndexOutOfBoundException

其次,您正在跳过第0个索引,因为NUM_FIELDS_Y =1将访问fields[1][1]而不是fields[0][0]

改变你的循环,

int xLength = 9;
int yLength = 9;
TextField[][] fields = new TextField[xLength][yLength];
for (int x = 0; x < xLength; x++) {
    for (int y = 0; y < yLength; y++) { 
        // your code
    }
}

修改

您的字段数组的长度为1

TextField[][] fields = new TextField[NUM_FIELDS_X][NUM_FIELDS_Y];

因此fields只能在索引[0][0]中有一个1元素,并且第一次迭代尝试访问不正确的[1][1]

访问this链接以了解有关数组的更多信息。

答案 1 :(得分:0)

您正在创建一个文本字段数组,如[1] [1]并且您尝试在数组中放置多个元素... 增加文本字段数组的大小。这将有所帮助