- 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/
我最近才开始学习 Clojure,所以很抱歉,如果这有点初级: 有人可以向我解释一下两者之间的区别吗: => (def a (lazy-cat [0]
我有一些看起来像这样的数据: X = [[1,2,3,4],[01010],[-1.6]] y = [[4,2]] 我正在尝试使用 tflearn 在这些数据上训练神经网络。我使用的是 TFlearn
我的代码有问题。 我正在尝试从 .txt 文件中提取 channel 名称。我不明白为什么方法 line.split() 给我返回一个长度为 0 的数组: 有人可以帮助我吗? 这是文件.txt: --
def sigmoid(z): # complete the code z = np.asarray(z) if z.ndim == 0: return(1/(1+np.exp(-z))) e
我在访问 3d 数组内的值时遇到问题。有时它给出正确的值,但有时它给出随机的数值。数组内不存在。 import java.util.*; public class Main { public
我有一段代码,执行时会出现此错误。而且我比较新,我似乎无法解决问题。 错误:2011-09-06 12:31:06.094 ForceGauge[266:707] CoreAnimation:忽略异常
我正在尝试限制 http://www.liftdesignphoto.com/test/ 中的滚动因为它让电梯超出了界限。 有没有办法重新计算位置,使其不越界? (也许使用 %)。 谢谢 最佳答案 假
我正在尝试遍历 6 个“国际象棋”棋子的列表。每轮他们移动一个随机数量,如果他们落在另一个上,他们就会“杀死”它。 问题是,当我的 vector 中的最后一 block 杀死另一 block 时,我收
NumberPicker serviceWheel = (NumberPicker) findViewById(R.id.serviceSelector); serviceWheel.setMaxVa
我正在尝试使用 GridLayout 重现此计算器布局 但这就是我用我尝试过的代码得到的结果。 事实上,在设备上情况会变得更糟,它会削减更多必须跨越两行的最后一个相等按钮。
运行测试脚本时出现“标签越界”错误。将注释值与类数进行比较时,confusion_matrix 函数会抛出错误。在我的例子中,注释值是一个图像(560x560)和 number_of_classes
什么是 OOL(越界)代码?我在 ION 编译器中找到了它,但无法理解发生了什么。 bool CodeGeneratorShared::generateOutOfLineCode() { for
这是我正在研究的有趣的事情。 varray.c: static GLint vertices[] = {25, 25, 100, 325,
我的程序将文件读取到字节数组中,然后尝试从该文件中提取 bmp 图像。问题是我遇到了越界错误。 { public static void main( String[] args ) {
我有一个 UITableView,它由从 XML 提要解析的数据数组填充。我正在努力寻找此错误的原因,并想知道是否有人可以帮助我。该错误不会经常发生。它仅在数组数量很大时发生,例如 10-15 个对象
public class GameEntry { private String name; private int score; public GameEntry(String
我遇到了 Storyboard的问题(至少有点惊讶)。 我有一个 ViewController,它包含一个容器 View 以及各种 ImageView 。自然地,选择的 ImageView 决定了容器
我正在尝试为一些 textfield 设置动画。即在屏幕外开始动画并移动到屏幕中央。但就我而言,动画从中心开始并超出 bounds。当我在 viewWillAppear/viewDidAppear 中
closeTs在struct tic给我一个错误 - tsP=0x66 .我尝试从 oracle 条目中填充它,如果没有,我尝试分配一个值。但我在 fillFields 中访问错误.有人可以给我提示
public class Registration { public static void main(String[] args) { final String MY
我是一名优秀的程序员,十分优秀!