Java 不';拖动时无法正确显示标签

Java 不';拖动时无法正确显示标签,java,swing,drag-and-drop,jlabel,mousemotionevent,Java,Swing,Drag And Drop,Jlabel,Mousemotionevent,拖动标签时,它只会显示在JPanel的左上角(板) 这是我的密码。但我从中复制的代码正在工作 Piece extends JLabel{/* code*/} 我的代码(大部分是复制的): 包棋; 导入java.awt.BorderLayout; 导入java.awt.Color; 导入java.awt.Component; 导入java.awt.Container; 导入java.awt.Dimension; 导入java.awt.GridLayout; 导入java.awt.LayoutMa

拖动标签时,它只会显示在JPanel的左上角(
) 这是我的密码。但我从中复制的代码正在工作

Piece extends JLabel{/* code*/}
我的代码(大部分是复制的):

包棋;
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Component;
导入java.awt.Container;
导入java.awt.Dimension;
导入java.awt.GridLayout;
导入java.awt.LayoutManager;
导入java.awt.Point;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入java.awt.event.MouseMotionListener;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JLayeredPane;
导入javax.swing.JPanel;
@抑制警告(“串行”)
公共类板扩展JFrame实现MouseListener、MouseMotionListener{
JLayeredPane;
JPanel董事会;
JLabel块;
int xAdjust;
国际雅贾斯特;
双白核;
双黑线;
弦乐冠军;
双倍时间;
工件白位;
工件黑点;
WhitePlayer wp;
BlackPlayer bp;
公共委员会(){
pane=新的JLayeredPane();
getContentPane()。添加(窗格);
窗格。设置首选尺寸(新尺寸(600600));
窗格。addMouseListener(本);
pane.addMouseMotionListener(此);
board=新的BoardArea();
添加(线路板,JLayeredPane.DEFAULT_层);
board.setLayout(新网格布局(8,8));
board.setPreferredSize(新尺寸(600600));
板.立根(0,0,600,600);
杰帕内尔广场;
对于(int i=0;i<8;i++){
对于(int j=0;j<8;j++){
正方形=新正方形(新边框布局(),j,i);
板。添加(方形);
正方形。背景(i%2==0?j%2==0?颜色。深灰色
:Color.WHITE:j%2==0?Color.WHITE
:颜色:深灰色);
}
}
wp=新的WhitePlayer();
对于(int i=0;i<16;i++){
((BoardArea)board.getPanel(wp.pieces[i].getX(),
wp.pieces[i].getY()).add(wp.pieces[i]);
}
}
私有类BoardArea扩展JPanel{
公共会议区(){
超级();
}
公共方形面板(整数x,整数y){
对于(int i=0;i<64;i++)
如果((平方)getComponent(i)).getX_index()=x
&&((平方)getComponent(i)).getY_index()=y)
返回(方形)组件(i);
返回null;
}
}
私人类广场延伸至JPanel{
私有整数x_指数;
私人综合指数;
公共广场(布局管理器布局,int x,int y){
超级(布局);
x_指数=x;
y_指数=y;
}
公共整数getX_索引(){
返回x_指数;
}
公共int getY_索引(){
返回y_指数;
}
}
@凌驾
公共无效mouseClicked(MouseEvent e){
}
@凌驾
公共无效鼠标按下(MouseEvent e){
工件=零;
组件c=board.findComponentAt(e.getX(),e.getY());
if(JPanel的c实例){
返回;
}
Point parentLocation=c.getParent().getLocation();
System.out.println(parentLocation.x-e.getX()+);
xAdjust=parentLocation.x-e.getX();
yAdjust=parentLocation.y-e.getY();
件=(件)c;
工件设置位置(e.getX()+xAdjust,e.getY()+yAdjust);
piece.setSize(piece.getWidth(),piece.getHeight());
添加(块,JLayeredPane.DRAG_层);
重新油漆();
}
@凌驾
公共无效鼠标标记(鼠标事件e){
如果(工件==null)
返回;
System.out.println(xAdjust+“”+yAdjust);
工件设置位置(e.getX()+xAdjust,e.getY()+yAdjust);
重新油漆();
}
@凌驾
public void mouseMoved(MouseEvent e){
}
@凌驾
公共无效MouseEvent(MouseEvent e){
如果(工件==null)
返回;
工件可见(假);
组件c=board.findComponentAt(e.getX(),e.getY());
容器母材;
if(工件的c实例){
parent=c.getParent();
父。删除(0);
添加(件);
}否则{
父项=(容器)c;
添加(件);
}
工件设置可见(真);
}
@凌驾
公共无效鼠标事件(鼠标事件e){
}
@凌驾
公共无效mouseExited(MouseEvent e){
}
公共静态void main(字符串[]args){
JFrame=新板();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setresizeable(false);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
}
工作代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class Main extends JFrame implements MouseListener, MouseMotionListener {
    JLayeredPane layeredPane;
    JPanel chessBoard;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;

    public Main() {
        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.DARK_GRAY : Color.white);
            else
                square.setBackground(i % 2 == 0 ? Color.white : Color.DARK_GRAY);
        }

        // Add a few pieces to the board

        JLabel piece = new JLabel(new ImageIcon("src/res/01.gif"));
        JPanel panel = (JPanel) chessBoard.getComponent(0);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/res/01.gif"));
        panel = (JPanel) chessBoard.getComponent(15);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/res/11.gif"));
        panel = (JPanel) chessBoard.getComponent(16);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/res/11.gif"));
        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();
        System.out.println(parentLocation.x + "" + e.getX());
        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);
        }

        chessPiece.setVisible(true);
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {

    }

    public static void main(String[] args) {
        JFrame frame = new Main();
        frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
@抑制警告(“串行”)
公共类Main扩展JFrame实现MouseListener、MouseMotionListener{
JLayeredPane layeredPane;
JPanel棋盘;
JLabel棋子;
内部调整;
内部调整;
公用干管(){
尺寸板尺寸=新尺寸(600600);
//为此应用程序使用分层窗格
layeredPane=新的JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
layeredPane.addMouseListener(此);
layeredPane.addMouseMotionListener(此);
//将棋盘添加到分层窗格
棋盘=新JPanel();
layeredPane.add(棋盘,JLayeredPane.DEFAULT_层);
棋盘布局(新网格布局(8,8));
棋盘。设置首选大小(棋盘大小);
棋盘.立根(0,0,boardSize.width,boardSize.height);
对于(int i=0;i<64;i++){
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class Main extends JFrame implements MouseListener, MouseMotionListener {
    JLayeredPane layeredPane;
    JPanel chessBoard;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;

    public Main() {
        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.DARK_GRAY : Color.white);
            else
                square.setBackground(i % 2 == 0 ? Color.white : Color.DARK_GRAY);
        }

        // Add a few pieces to the board

        JLabel piece = new JLabel(new ImageIcon("src/res/01.gif"));
        JPanel panel = (JPanel) chessBoard.getComponent(0);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/res/01.gif"));
        panel = (JPanel) chessBoard.getComponent(15);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/res/11.gif"));
        panel = (JPanel) chessBoard.getComponent(16);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("src/res/11.gif"));
        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();
        System.out.println(parentLocation.x + "" + e.getX());
        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);
        }

        chessPiece.setVisible(true);
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {

    }

    public static void main(String[] args) {
        JFrame frame = new Main();
        frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}