gpt4 book ai didi

java - 要求java中有关jlabel和parent的一些澄清

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

在互联网上找到了这段代码,它是几年前发布的,所以我决定在这里要求对一些我不太理解的行进行一些澄清。

mousePressed方法,他的意思是: chessPiece = null他是说如果 JLabel chessPiece其中有图像,则应将其更改为 null

chessBoard.findComponentAt(e.getX(), e.getY())返回 JPanel正方形?

最后,当Component 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 可以搜索当前容器及其任何子容器,直到在指定位置找到组件。从技术上讲,作者忽略了触发事件的组件(应该是 layeredPane),而是在 chessBoard 上行走。我怀疑他们这样做是因为如果他们使用layeredPane,它将返回chessBoard

该方法能够返回 JPanelJLabel 甚至可能返回 null,但考虑到组件的布局方式,它是概率较低。

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

这要看情况。根据我对代码的理解,我会说它在 JLabel 部分下面返回一个 JPanel

不得不说,有更简单的方法可以达到相同的结果......

关于java - 要求java中有关jlabel和parent的一些澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22343047/

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