请求java中有关jlabel和parent的一些说明 [英] Asking for some clarification in java about jlabel and parent
问题描述
在互联网上发现这个代码,它是在几年前发布的,所以我决定在这里询问一些我不太了解的行的澄清。
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.
在 mousePressed
方法中,他的意思是:
chessPiece = null
他是说如果 JLabel chessPiece
中有一个图像然后应该更改为 null
?
In the mousePressed
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
?
chessBoard.findComponentAt(e.getX(),e.getY())
返回 JPanel
square?
Is chessBoard.findComponentAt(e.getX(), e.getY())
returns the JPanel
square?
最后,当组件c
获取其父级时,谁是父级?
and lastly, when Component c
gets its parent, who is the parent?
整个代码如下:
public class ChessGameDemo extends JFrame implements MouseListener, MouseMotionListener {
JLayeredPane layeredPane;
JPanel chessBoard;
JLabel chessPiece;
int xAdjustment;
int yAdjustment;
private static final String imageFolderPath = "src/resources/images/";
public ChessGameDemo() {
Dimension boardSize = new Dimension(600, 600);
// Use a Layered Pane for this this application
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
layeredPane.addMouseListener(this);
layeredPane.addMouseMotionListener(this);
//Add a chess board to the Layered Pane
chessBoard = new JPanel();
layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
chessBoard.setLayout(new GridLayout(8, 8));
chessBoard.setPreferredSize(boardSize);
chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
for (int i = 0; i < 64; i++) {
JPanel square = new JPanel(new BorderLayout());
chessBoard.add(square);
int row = (i / 8) % 2;
if (row == 0) {
square.setBackground(i % 2 == 0 ? Color.blue : Color.white);
} else {
square.setBackground(i % 2 == 0 ? Color.white : Color.blue);
}
}
//Add a few pieces to the board
JLabel piece = new JLabel(new ImageIcon(imageFolderPath + "/pieces/bdg.png"));
JPanel panel = (JPanel) chessBoard.getComponent(0);
panel.add(piece);
piece = new JLabel(new ImageIcon(imageFolderPath + "/pieces/belder.png"));
panel = (JPanel) chessBoard.getComponent(15);
panel.add(piece);
piece = new JLabel(new ImageIcon(imageFolderPath + "/pieces/bhero.png"));
panel = (JPanel) chessBoard.getComponent(16);
panel.add(piece);
piece = new JLabel(new ImageIcon(imageFolderPath + "/pieces/borb.png"));
panel = (JPanel) chessBoard.getComponent(20);
panel.add(piece);
}
public void mousePressed(MouseEvent e) {
chessPiece = null;
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
if (c instanceof JPanel) {
return;
}
Point parentLocation = c.getParent().getLocation();
xAdjustment = parentLocation.x - e.getX();
yAdjustment = parentLocation.y - e.getY();
chessPiece = (JLabel) c;
chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
}
//Move the chess piece around
public void mouseDragged(MouseEvent me) {
if (chessPiece == null) {
return;
}
chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
}
//Drop the chess piece back onto the chess board
public void mouseReleased(MouseEvent e) {
if (chessPiece == null) {
return;
}
chessPiece.setVisible(false);
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
if (c instanceof JLabel) {
Container parent = c.getParent();
parent.remove(0);
parent.add(chessPiece);
} else {
Container parent = (Container) c;
parent.add(chessPiece);
}
....
}
推荐答案
在
mousePiece
方法中,他的意思是:chessPiece = null
是
他说如果JLabel chessPiece
中有图像,那么
应改为null
?
In the
mousePiece
method, what does he mean by:chessPiece = null
is he saying that if theJLabel chessPiece
has a image in it then it should be changed tonull
?
我假设你是指 mousePressed
。通过使用 chessPiece = 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
是
chessBoard.findComponentAt(e.getX(),e.getY())
返回JPanel
square?
Is
chessBoard.findComponentAt(e.getX(), e.getY())
returns theJPanel
square?
这取决于。 findComponentAt
可以搜索当前容器及其任何子容器,直到找到指定位置的组件。 Technquial,作者忽略了触发事件的组件(应该 layeredPane
)并且正在走 layeredPane
,它将返回 chessBoard
。
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.
该方法能够返回 JPanel
, JLabel
甚至可能 null
,但考虑到组件布局的方式,它的概率较低。
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.
和最后,当组件c获得其父项时,谁是父项?
and lastly, when Component c gets its parent, who is the parent?
这取决于。根据我对代码的理解,我会说它返回 JPanel
在 JLabel
片之下。
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...
这篇关于请求java中有关jlabel和parent的一些说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!