【问题标题】:Java out of bounds error but I am sure it's declared properlyJava 越界错误,但我确信它已正确声明
【发布时间】:2014-10-31 16:04:04
【问题描述】:

此 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)

【问题讨论】:

  • 能否添加异常跟踪以及代码段中显示异常的行?
  • 我添加了它。它在第 140 行。
  • 我们不知道第 140 行是哪一行。如果我将其复制到编辑器中,第 140 行只有一个}。我想我们可以知道它是哪一个,但下次请在您的来源中添加评论,告诉我们引用了哪一行。

标签: java arrays swing user-interface


【解决方案1】:

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

所以当你在actionPerformed 中选择for(int j = 0; j &lt; col; j++) 时,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 加一,我个人认为这很奇怪,

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

【讨论】:

  • 还是不行。当 col 到达 actionPerformed 函数时,它会为自己添加一个,这很奇怪。
  • 我同意 Xavier 的观点,即您没有确切的数组元素。
  • 我已经尝试在 for 循环中使用 this.col。它仍然无法正常工作。
  • 好的。我删除了它。但是,它仍然为自己增加了一个。我删除了 +1。
  • Jessie,你确定你还在重现 ArrayIndexOutOfBoundsException 吗?
【解决方案2】:

丢弃类变量(行、列)并根据数组大小进行迭代。创建数组后,无需维护行和列变量。

    for (int i = 0; i < cells.length; i++)
        for (int j = 0; j < cells[i].length; j++) {
            System.out.println(i + " " + j);
            cells[i][j].setText("");
        }

按预期工作。

【讨论】:

  • 您的想法要好得多,但我仍然不明白为什么它会为自己添加一个。哈哈。
猜你喜欢
  • 2014-06-10
  • 1970-01-01
  • 2016-04-12
  • 1970-01-01
  • 2020-09-15
  • 2013-10-13
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多