java jlabel如何继承_请求java中有关jlabel和parent的一些说明

This blog post discusses a Java chess game implementation where the author explores the code behind mouse interactions with JLabels representing chess pieces. It explains the purpose of setting chessPiece to null, the usage of chessBoard.findComponentAt() method, and identifying the parent component of a given chess piece." 111889542,10537112,layui表格排序详解:前端与服务器排序策略,"['layui', '前端开发', '表格排序', '数据处理', 'JavaScript']
摘要由CSDN通过智能技术生成

在互联网上发现了这个代码,它是在几年前发布的,所以我决定在这里要求澄清一些我不太了解的行.

在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值