拖动时不会正确显示标签

时间:2013-04-23 07:44:38

标签: java swing drag-and-drop jlabel mousemotionevent

当您拖动标签时,它只会将其显示在JPanel的左上角(board) 这是我的代码。但是我复制的代码正在运行。

Piece extends JLabel{/* code*/}

我的代码(大多数是复制的):

package chess;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Board extends JFrame implements MouseListener, MouseMotionListener {

    JLayeredPane pane;
    JPanel board;
    JLabel piece;
    int xAdjust;
    int yAdjust;

    double whiteScore;
    double Blackscore;
    String winner;
    double time;
    Piece whitePosition;
    Piece blackPosition;
    WhitePlayer wp;
    BlackPlayer bp;

    public Board() {
        pane = new JLayeredPane();
        getContentPane().add(pane);
        pane.setPreferredSize(new Dimension(600, 600));
        pane.addMouseListener(this);
        pane.addMouseMotionListener(this);

        board = new BoardArea();
        pane.add(board, JLayeredPane.DEFAULT_LAYER);
        board.setLayout(new GridLayout(8, 8));
        board.setPreferredSize(new Dimension(600, 600));
        board.setBounds(0, 0, 600, 600);
        JPanel square;
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                square = new Square(new BorderLayout(), j, i);
                board.add(square);
                square.setBackground(i % 2 == 0 ? j % 2 == 0 ? Color.DARK_GRAY
                        : Color.WHITE : j % 2 == 0 ? Color.WHITE
                        : Color.DARK_GRAY);
            }
        }

        wp = new WhitePlayer();
        for (int i = 0; i < 16; i++) {
            ((BoardArea) board).getPanel(wp.pieces[i].getX(),
                    wp.pieces[i].getY()).add(wp.pieces[i]);
        }
    }

    private class BoardArea extends JPanel {

        public BoardArea() {
            super();
        }

        public Square getPanel(int x, int y) {
            for (int i = 0; i < 64; i++)
                if (((Square) getComponent(i)).getX_index() == x
                        && ((Square) getComponent(i)).getY_index() == y)
                    return (Square) getComponent(i);
            return null;
        }

    }

    private class Square extends JPanel {
        private int x_index;
        private int y_index;

        public Square(LayoutManager layout, int x, int y) {
            super(layout);
            x_index = x;
            y_index = y;
        }

        public int getX_index() {
            return x_index;
        }

        public int getY_index() {
            return y_index;
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        piece = null;
        Component c = board.findComponentAt(e.getX(), e.getY());

        if (c instanceof JPanel) {
            return;
        }

        Point parentLocation = c.getParent().getLocation();
        System.out.println(parentLocation.x - e.getX() + "");
        xAdjust = parentLocation.x - e.getX();
        yAdjust = parentLocation.y - e.getY();
        piece = (Piece) c;
        piece.setLocation(e.getX() + xAdjust, e.getY() + yAdjust);
        piece.setSize(piece.getWidth(), piece.getHeight());
        pane.add(piece, JLayeredPane.DRAG_LAYER);
        repaint();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (piece == null)
            return;
        System.out.println(xAdjust + " " + yAdjust);
        piece.setLocation(e.getX() + xAdjust, e.getY() + yAdjust);
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (piece == null)
            return;
        piece.setVisible(false);
        Component c = board.findComponentAt(e.getX(), e.getY());
        Container parent;

        if (c instanceof Piece) {
            parent = c.getParent();
            parent.remove(0);
            parent.add(piece);
        } else {
            parent = (Container) c;
            parent.add(piece);
        }

        piece.setVisible(true);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    public static void main(String[] args) {
        JFrame frame = new Board();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        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);
    }
}

0 个答案:

没有答案