几乎是标题。我尝试使用 AtomicBoolean 作为标志,这样 mouseListener 方法只执行一次,这对 mousePressed 很有效,但对 mouseReleased 就不太适用了。此外,每次释放鼠标时,mouseReleased 打印语句似乎都会复合,我不确定为什么。我当前的代码如下,任何帮助将不胜感激!
//Creates board of desired dimensions and colour
public static void createBoard() {
//Sets desired colour for the board background
tileRGBCode(colourScheme);
//Creates empty JFrame of user's chosen dimensions to hold checkerboard
boardFrame.setSize(rowLength * 64, rowLength * 64);
boardFrame.setResizable(false);
boardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Creates panel for checkerboard
JPanel structure = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
//Creates checkerboard of user's chosen colour and size
boolean pieceTile = true;
for (int i = 0; i < rowLength; i++) {
for (int j = 0; j < rowLength; j++) {
if (pieceTile) {
g.setColor(new Color(RGBCodes[0], RGBCodes[1],
RGBCodes[2]));
} else {
g.setColor(new Color(RGBCodes[3], RGBCodes[4],
RGBCodes[5]));
}
g.fillRect(i * 64, j * 64, 64, 64);
pieceTile = !pieceTile;
}
pieceTile = !pieceTile;
}
//Creates pieces to fit on desired board size
for (int i = 0; i < piecePositions.length; i++) {
//Creates board for Draughts/Checkers
if (rowLength == 8) {
if (i % 2 != 0) {
//Creates pieces for player one
if (i < 24) {
g.setColor(Color.RED);
//Creates pieces for player one
g.fillOval((piecePositions[i - 1] - 1) * 64 + 8,
(piecePositions[i] - 1) * 64 + 8,
48, 48);
//Creates pieces for player two
} else {
g.setColor(Color.RED);
//Creates pieces for player two
g.fillRoundRect((piecePositions[i - 1] - 1) * 64
+ 8, (piecePositions[i] - 1) * 64 + 8,
48, 48,
32, 32);
}
}
} else {
//Creates board for International Draughts
if (i < 4) {
g.setColor(new Color(RGBCodes[3],
RGBCodes[4], RGBCodes[5]));
g.fillOval(i * 64 + 8, i * 64 + 8,
48, 48);
} else {
g.setColor(new Color(RGBCodes[3],
RGBCodes[4], RGBCodes[5]));
g.fillRoundRect(i * 64 + 8, i * 64 + 8,
48, 48, 32,
32);
}
}
}
//Tracks where user clicks and releases mouse
boardFrame.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
//Ensures outputs are only displayed once
if (!isButtonPressed.compareAndExchange
(false, true)
&& e.getButton() == 1) {
int x1 = (e.getLocationOnScreen().x) / 64 - 10;
int y1 = (e.getLocationOnScreen().y - 20) / 64 - 3;
mouseListenerCoords[0] = x1;
mouseListenerCoords[1] = y1;
System.out.println("Pressed: " + x1 + ", " + y1);
}
}
public void mouseReleased(MouseEvent e) {
//Ensures outputs are only displayed once
if (e.getButton() == 1) {
isButtonPressed.compareAndExchange(
true, false);
}
//Changed which player's turn it is
int x2 = (e.getLocationOnScreen().x) / 64 - 10;
int y2 = (e.getLocationOnScreen().y - 20) / 64 - 3;
mouseListenerCoords[2] = x2;
mouseListenerCoords[3] = y2;
System.out.println("Released: " + x2 + ", " + y2);
//Changes positions of piece, if selected and moved
piecePositionChanger();
//Recreates board with pieces in new positions
createBoard();
}
});
}
};
//Adds checkerboard to JFrame frame
boardFrame.add(structure);
//Sets frame to be visible
boardFrame.setVisible(true);
}