我有一个JPanel
s的数组,我试图给每一个添加一个mouseadapter,以便可以识别哪个被点击,然后更改它的背景颜色。一切似乎当我从当一个页面会出现日食运行它除了做工精细说EventDispatchThread.run()
行:不可用,找不到源代码,并在调试窗口,它说:java mouseadapter源未找到
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
但它不应该是一个反正这个事实?如果我不放置演员,它说它不能绑定一个'对象'到JPanel
,当我做System.out.print(me.getSource())
时,它打印出一条线,说这是一个JPanel
,所以我不明白问题所在。任何帮助将非常感激!
在Eclipse中将源代码附加到Java运行时的最简单方法是确保您的项目使用JDK而不是JRE。您可以在Window-> Preferences-> Java-> Installed JRE中配置Java的可用版本。使JDK成为默认设置,调试应该更容易。 – 2010-01-31 16:06:11
非常感谢:D这摆脱了源未找到错误! – Becky 2010-01-31 23:31:22