gpt4 book ai didi

未找到 java mouseadapter 源

转载 作者:行者123 更新时间:2023-12-01 19:24:31 25 4
gpt4 key购买 nike

我有一组 JPanel,我正在尝试为每个 JPanel 添加一个鼠标适配器,以便可以识别单击了哪个 JPanel,然后更改其背景颜色。一切似乎都工作正常,除了当我从 Eclipse 运行它时,会出现一个页面,上面写着 EventDispatchThread.run() line: not available, Source not find,并且在调试窗口中显示:

  Thread [AWT-Shutdown] (Running) 
Daemon Thread [AWT-Windows] (Running)
Thread [AWT-EventQueue-0] (Suspended (exception ArrayIndexOutOfBoundsException))
EventDispatchThread.run() line: not available
Thread [DestroyJavaVM] (Running)
Thread [AWT-EventQueue-0] (Running)

这是代码:

private void drawBoard() {
LayoutManager layout = new GridLayout(NUMBER_OF_ROWS, NUMBER_OF_COLS);
boardPanel.setLayout(layout);
boardPanel.setPreferredSize(new Dimension(200, 400));
chessBoard = new JPanel[NUMBER_OF_ROWS][NUMBER_OF_COLS];
MoveArrays move = new MoveArrays();
move.initialisePieceMoves();
for (int i = 0; i < NUMBER_OF_ROWS; i++) {
for (int j = 0; j < NUMBER_OF_COLS; j++) {
int index = i * 4 + j;
chessBoard[i][j] = new JPanel();
chessBoard[i][j].addMouseListener(clickSquare(j, i, index, move));
chessBoard[i][j].setBackground(getColor(i,j));
if (!(boardArray.chessBoard[index].square.isEmpty())) {
Piece piece = (Piece) boardArray.chessBoard[index].square.firstElement();
JLabel pieceString = new JLabel(piece.toString());
chessBoard[i][j].add(pieceString);
}
boardPanel.add(chessBoard[i][j]);
}
}
} // drawBoard()

private MouseAdapter clickSquare(final int xCo, final int yCo, final int index, final MoveArrays move) {
return new MouseAdapter() {
public void mousePressed(MouseEvent me) {
resetColors();
JPanel selectedSquare = (JPanel) me.getSource();
selectedSquare.setBackground(selectedColor());
System.out.println("xCo: " + xCo + " yCo: " + yCo);
Vector validMoves = move.DroneMovesNorth[index].Moves;
int totalMoves = move.DroneTotalMovesNorth[index];
if (!validMoves.isEmpty()) {
for (int n = 0; n <= totalMoves; n++) {
String stringMove = validMoves.elementAt(n).toString();
int intMove = Integer.parseInt(stringMove);
System.out.println("intMove: " + intMove);
}
}
}
};
}

我认为这可能是因为我将 me.getSource 转换为 JPanel 但它不应该是一个吗?如果我不进行强制转换,它会说它无法将“对象”绑定(bind)到 JPanel,当我执行 System.out.print(me.getSource()) 时> 它打印一行说它是一个 JPanel 所以我不明白问题是什么。任何帮助将不胜感激!

最佳答案

看来问题与鼠标事件的来源无关。事实上,我认为这里有两种不同的东西被称为“来源”,而你将它们混合在一起。我想传达的信息是:

EventDispatchThread.run() line: not available, Source not found

Eclipse 是否告诉您该库没有附加源代码,因此它无法找到行号或向您显示源代码。问题似乎不在于 me.getSource()

问题在于您试图引用数组中超出数组范围的索引(因此堆栈跟踪中出现 ArrayIndexOutOfBoundsException)。

由于堆栈跟踪位于 AWT 事件队列线程上,因此异常可能源自 mousePressed() 内。你的方法MouseAdapter 。由于您已经在使用 Eclipse,我建议您了解一下调试器,它非常有用。在这些行处放置断点:

Vector validMoves = move.DroneMovesNorth[index].Moves;
int totalMoves = move.DroneTotalMovesNorth[index];

并检查 move 的两个数组字段足够大以引用索引处的元素。如果您不想使用调试器(我真的推荐这种方式),那么您可以用异常捕获来包装这两行,如下所示:

try {
Vector validMoves = move.DroneMovesNorth[index].Moves;
int totalMoves = move.DroneTotalMovesNorth[index];
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Exception thrown: index = " + index +
"Array lengths: " + move.DroneMovesNorth.length + ", " +
move.DroneTotalMovesNorth.length);
}

当/如果捕获异常时,您想要找出为什么索引大于每个数组的大小。这留作练习;-)

<小时/>

编辑:其中还有一些看起来可疑的其他代码。

首先声明棋盘的数组:

chessBoard = new JPanel[NUMBER_OF_ROWS][NUMBER_OF_COLS];

然后你开始迭代棋盘中的每个方 block :

for (int i = 0; i < NUMBER_OF_ROWS; i++) {
for (int j = 0; j < NUMBER_OF_COLS; j++) {

请注意i将运行到值 NUMBER_OF_ROWS ,这是棋盘数组的长度。然后由于某种我不明白的原因,你改变了 index 的值可能是数组长度的 4 倍:

int index = i * 4 + j;

稍后尝试引用棋盘数组中的该位置:

if (!(boardArray.chessBoard[index].square.isEmpty())) {

这意味着 index 是可能的为高于 NUMBER_OF_ROWS 的值,如果您尝试访问数组该索引处的元素,它将抛出 ArrayIndexOutOfBoundsException .

因此,我的第二个建议是重新审视其中涉及的逻辑:

int index = i * 4 + j;

...因为这也可能是问题所在。

附注Eclipse 中的调试器非常棒,您应该使用它;-)

关于未找到 java mouseadapter 源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2171830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com