在互联网上发现了这个代码,它是在几年前发布的,所以我决定在这里要求澄清一些我不太了解的行.
在mousePressed方法中,他的意思是:
chessPiece = null他说如果JLabel chessPiece有一个图像,那么它应该改为null?
chessBoard.findComponentAt(e.getX(),e.getY())是否返回JPanel方块?
最后,当组件c获得其父级时,谁是父级?
整个代码如下:
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);
}
....
}
解决方法:
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?
我假设你的意思是mousePressed.通过使用chessPiece = null,作者将取消引用变量,因此通过is变量无法再访问分配给它的变量
Is chessBoard.findComponentAt(e.getX(), e.getY()) returns the JPanel square?
这取决于. findComponentAt可以搜索当前容器,它是任何子容器,直到找到指定位置的组件. Technquial,作者忽略了触发事件的组件(应该是layeredPane)并且正在走向chessBoard.我怀疑他们这样做是因为如果他们使用layeredPane它会返回chessBoard.
该方法能够返回JPanel,JLabel甚至可能返回null,但是考虑到组件布局的方式,它的概率较低.
and lastly, when Component c gets its parent, who is the parent?
这取决于.基于我对代码的理解,我会说它返回Jaabel片下的JPanel.
不得不说,虽然有更简单的方法可以达到相同的效果……
标签:java,swing,parent,jlabel,mouseevent
来源: https://codeday.me/bug/20190929/1832341.html