作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个 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/
我是一名优秀的程序员,十分优秀!