好的事件監聽器添加一個JLabel以JLayeredPane的,所以,由於某種原因,當我在構造函數中添加組件的JLayeredPane的:從不拖拉
JLabel label = new JLabel();
label.setSize(100,100); label.setText(「This works」); add(label); 它工作得很好,但如果稍後在JLayeredPane的父級EDT中添加它,它不會讓我移動對象,但它們讓我看到對象。
從EDT添加:
JLabel label = new JLabel();
label.setToolTipText(url.getHost());
label.setIcon(icon);
label.setBorder(new LineBorder(null));
label.setSize(icon.getIconWidth(), icon.getIconHeight());
dressFrame.layeredPane.add(label, JLayeredPane.DRAG_LAYER);
拖動方法:
Component c = findComponentAt(e.getX(), e.getY());
if (c instanceof JLayeredPane) {
pieceSelected = false;
return;
}
Point parentLocation = c.getLocation();
xAdjustment = parentLocation.x - e.getX();
yAdjustment = parentLocation.y - e.getY();
movingPiece = c;
movingPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
pieceSelected = true;
http://xslayer.com/upload/gui.png
請提供更多代碼,特別是MouseMotionListener的註冊 – fish 2010-04-25 05:14:06