Asking for some clarification in java about jlabel and parent

前端 未结 2 1023
名媛妹妹
名媛妹妹 2020-12-11 13:57

Found this code in the internet, it was posted years ago, so I just decided to ask here for some clarifications for some lines I don\'t quite understand.

In the

2条回答
  •  既然无缘
    2020-12-11 14:20

    In the mousePiece method, what does he mean by: chessPiece = null is he saying that if the JLabel chessPiece has a image in it then it should be changed to null?

    I assume you mean mousePressed. By using chessPiece = null, the author is de-referencing the variable, so what ever was assigned to it is no longer reachable through the is variable

    Is chessBoard.findComponentAt(e.getX(), e.getY()) returns the JPanel square?

    This depends. findComponentAt can search the current container and it's any of it's child containers until it finds a component at the specified position. Technquial, the author is ignoring the component that triggered the event (which should layeredPane) and is walking the chessBoard instead. I suspect they are doing this because if they used layeredPane it would return chessBoard instead.

    The method is capable of returning JPanel, JLabel and possibly even null, but given the way that the components are laid out, it's a lower probability.

    and lastly, when Component c gets its parent, who is the parent?

    This depends. Based on my understanding of the code, I would say it's return a JPanel underneth the JLabel piece.

    Have to say, there are easier ways to achieve the same result though...

提交回复
热议问题