gpt4 book ai didi

java - 不兼容类型 Piece 无法转换为 java 中的 mouselistener

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

我面临问题,我有从 JButton 扩展的 Piece 类,并且在该函数中的 Piece 类中有 Promotion 函数,我正在添加鼠标监听器,但收到此错误,

incompatible type Piece can not convert into mouselistener

这里是推广功能

public void promote()
{

pawnPromotion.setTitle("Pawn Promotion interface");
pawnPromotion.setSize(new Dimension(400, 200));
pawnPromotion.setResizable(false);
JPanel mainpanel = new JPanel(new GridLayout(1, 5));
final Piece king = new King(this.getBoard().getPieceToMove().isWhite(),this.getBoard().getboard());
final Piece queen = new Queen(this.getBoard().getPieceToMove().isWhite(),this.getBoard().getboard());
final Piece bishop = new Bishop(this.getBoard().getPieceToMove().isWhite(),this.getBoard().getboard());
final Piece knight = new Knight(this.getBoard().getPieceToMove().isWhite(),this.getBoard().getboard());
final Piece rook = new Rook(this.getBoard().getPieceToMove().isWhite(),this.getBoard().getboard());
addMouseListnerq(this, queen);
addMouseListnerq(this, bishop);
addMouseListnerq(this, knight);
addMouseListnerq(this, rook);
mainpanel.add(king);
mainpanel.add(queen);
mainpanel.add(bishop);
mainpanel.add(knight);
mainpanel.add(rook);
pawnPromotion.add(mainpanel);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
pawnPromotion.setLocation(dim.width/2-pawnPromotion.getSize().width/2, dim.height/2-pawnPromotion.getSize().height/2);
pawnPromotion.show();

}

这是添加鼠标监听器功能的代码

public void addMouseListnerq(final Piece ClickedPiece,final Piece impPiece)
{

impPiece.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
Piece piece = new Piece(null);
if(impPiece instanceof Queen) piece = new Queen(getBoard().getPieceToMove().isWhite(),getBoard().getboard());
if(impPiece instanceof Bishop) piece = new Bishop(getBoard().getPieceToMove().isWhite(),getBoard().getboard());
if(impPiece instanceof Knight) piece = new Knight(getBoard().getPieceToMove().isWhite(),getBoard().getboard());
if(impPiece instanceof Rook) piece = new Rook(getBoard().getPieceToMove().isWhite(),getBoard().getboard());
piece.setBackground(ClickedPiece.getOldBackgroundColor());
piece.setOldBackgroundColor(ClickedPiece.getOldBackgroundColor());
addMouseListener(piece);
if(ClickedPiece.HasPiece()) getBoard().kill(ClickedPiece, getBoard().getPieceToMove(), getBoard().getAllSquares());
else getBoard().move(ClickedPiece, getBoard().getPieceToMove(), getBoard().getAllSquares());
int indextofPawn =0;
indextofPawn = getBoard().getAllSquares().indexOf(getBoard().getPieceToMove());
getBoard().getAllSquares().remove(getBoard().getPieceToMove());
getBoard().getAllSquares().add(indextofPawn, piece);
getBoard().resetColors();
getBoard().updateGui();
getBoard().deSelect();
setSelected(false);
getBoard().setPieceToMove(null);
if(getBoard().isWhiteplayerturn())
{ getBoard().setWhiteplayerturn(false); }
else
{ getBoard().setWhiteplayerturn(true); }
pawnPromotion.hide();

}});

}

最佳答案

方法addMouseListener需要一个MouseListener类型的对象

删除addMouseListener(piece);

或者在Piece类中实现接口(interface)MouseListener

希望这有帮助

关于java - 不兼容类型 Piece 无法转换为 java 中的 mouselistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27382579/

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