Java 越界错误,但我确信它已正确声明

标签 java arrays swing user-interface

这个 GUI 是由 javax.swing 制作的。我想知道为什么它会抛出 Array Index Out of Bounds 异常,但我确定它已正确声明。我还尝试在单元格实例化和单元格重置的 for 循环内运行测试,但它仍然无法正常工作。当我尝试在 resetFields actionEvent 中打印循环的 i 和 j 时,它并没有真正越界,但它说它越界了。

我发现 col 在执行 actionPerformed 函数时会为自身加一,这有点奇怪。

注意:我从另一个 Java 文件的主类中获取行和列。

完整代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ExperimentalFrame extends JFrame implements ActionListener
{
    private int row, col;
    private JTextField cells[][];
    private JPanel matrix;
    private JButton compute1;
    private JButton reset;
    private JButton showSolution1;
    private JButton resetFields;
    private JPanel matrixPanel;
    private JPanel mainPanel;
    private JPanel buttonPanel;
    private JLabel theory;
    private JPanel theoryPanel;
    private JLabel header[];

    public ExperimentalFrame(int row, int col)
    {
        this.row = row; this.col = col;
        theoryPanel = new JPanel();
        theory = new JLabel("Theory here");
        mainPanel = new JPanel();
        matrix = new JPanel();
        matrixPanel = new JPanel();
        buttonPanel = new JPanel();
        cells = new JTextField[row][col];
        header = new JLabel[col];
        compute1 = new JButton("Compute");
        reset = new JButton("Reset");
        showSolution1 = new JButton("Show Solution");
        resetFields = new JButton("Reset Fields");
        GridBagConstraints gbc = new GridBagConstraints();
        GridBagConstraints gbc2 = new GridBagConstraints();
        GridBagConstraints gbc1 = new GridBagConstraints();
        matrixPanel.setLayout(new GridBagLayout());
        matrix.setLayout(new GridBagLayout());
        mainPanel.setLayout(new GridBagLayout());
        theoryPanel.setLayout(new GridBagLayout());
        buttonPanel.setLayout(new GridBagLayout());

        compute1.addActionListener(this);
        reset.addActionListener(this);
        showSolution1.addActionListener(this);
        resetFields.addActionListener(this);

        gbc1.gridx = 1;
        gbc1.gridy = 1;
        gbc1.insets = new Insets(10,10,10,10);
        theoryPanel.add(theory);
        mainPanel.add(theoryPanel, gbc1);

        for (int j = 0; j < col; j++)
        {
            gbc.gridy = 1;
            gbc.gridx = j + 2;
            if (j != (col - 1))
                header[j] = new JLabel("I" + (j + 1));
            else
                header[j] = new JLabel("x");
            matrix.add(header[j], gbc);
        }

        gbc.insets = new Insets(5, 5, 5, 5);

        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                gbc.gridx = j + 2;
                gbc.gridy = i + 3;
                cells[i][j] = new JTextField(3);
                System.out.println(i + " " + j);
                matrix.add(cells[i][j], gbc);
            }
        }

        for (int i = 0; i < row; i++)
        {
            gbc.gridx = 1;
            gbc.gridy = i + 3;
            matrix.add(new JLabel("Equation " + (i + 1) + ": "), gbc);
        }

        gbc2.insets = new Insets(10, 10, 10, 10);
        gbc1.gridx = 1;
        gbc1.gridy = 1;
        gbc1.insets = new Insets(10,10,10,10);
        matrixPanel.add(matrix, gbc1);
        gbc1.gridy = 2;
        gbc1.insets = new Insets(0, 10, 10, 10);
        matrixPanel.add(compute1, gbc1);
        matrixPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
        gbc1.gridx = 1;
        gbc1.gridy = 2;
        mainPanel.add(matrixPanel, gbc1);
        //gbc1.gridy = 3;

        gbc1.anchor = GridBagConstraints.LINE_END;
        gbc1.gridx = 1; gbc1.gridy = 1;
        gbc1.insets = new Insets(0, 5, 0, 0);
        buttonPanel.add(reset, gbc1);
        gbc1.gridx = 2; gbc1.gridy = 1;
        buttonPanel.add(resetFields, gbc1);

        gbc1.gridx = 1; gbc1.gridy = 3;
        gbc1.insets = new Insets(0, 10, 10, 10);
        mainPanel.add(buttonPanel, gbc1);
        add(mainPanel);

        pack();
        setTitle("Experimental");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent ae)
    {
        Object o = ae.getSource();
        JButton jb = (JButton) o;
        if (jb == reset)
        {
            this.setVisible(false);
            this.dispose();
            new Experiment10();
        }
        else if (jb == resetFields)
        {
            System.out.println(row + " " + col);
            for (int i = 0; i < row; i++)
                for (int j = 0; j < col; j++)
                {
                    System.out.println(i + " " + j);
                    cells[i][j].setText(""); //here
                }
        }
    }
}

这是错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException:6
    at ExperimentalFrame.actionPerformed(ExperimentalFrame.java:140)

最佳答案

这是因为,当你声明cells = new JTextField[row][col]; col不等于 this.col ( this.col = col + 1 )

所以当你去寻找for(int j = 0; j < col; j++)actionPerformed j超出了 this.cells 的范围

例如,当您使用 col = 5 实例化您的类时,将创建宽度为 5 的单元格,但是,this.col 将等于 6,并且 j 将在您的 for 循环中使用 5 值。

要纠正你的问题,这样做

public ExperimentalFrame(int row, int col){
        this.row = row; this.col = col;

或者,如果你真的需要将 col 增加 1,我个人认为这很奇怪,

public ExperimentalFrame(int row, int col){
        col++;
        this.row = row; this.col = col;

关于Java 越界错误,但我确信它已正确声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26678869/

相关文章:

java - Java 的可扩展 GUI 框架

java - 错误扫描文本文件显示错误 "Exception in thread "AWT-EventQueue- 0"java.util.NoSuchElementException"

arrays - 算法 - LinkedIn Coding Round

javascript - 选择/取消选择单元格时从数组中添加/删除项目

java - 无法从 ActionPerformed 调用构造函数组件

java - 在没有 null 布局的 Swing 中绕过组件的布局

java - 递归计算PI

java - 如何使用 joda time 检查时间是否在间隔之前

java - 新手: how to save and print objects in Java?

ruby-on-rails - 从 ruby​​ 中的数组中删除重复项