java jlabel需要实例化么_在Java中要求有关jlabel和parent的一些澄清

在互联网上找到了此代码,它是在多年前发布的,因此,我决定在这里询问一些我不太了解的行的说明。

在该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);

}

....

}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值