它们是使jlabel可移动的一种方法吗?

lf5gs5x2  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(484)

我正在尝试用可拖动的棋子制作一个棋盘,我添加了一个鼠标运动监听器,每次拖动时,棋子都不会移动。例如,如果我将我的白色棋子拖到a4位置,棋子将不会移动并保持原位。因为我添加了鼠标运动监听器并使jlabel成为静态的,所以它删除了jlabel(一个黑棋子)的图标,所以添加运动监听器是行不通的。
我以前的代码,

import java.awt.EventQueue;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

import javax.swing.JPanel;
import javax.swing.Timer;

import java.awt.Color;
import javax.swing.JLabel;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

public class Chessboard {

    private JFrame frame;
    private static List <String> blackFileLoc = new ArrayList<String>();
    private static List <String> whiteFileLoc = new ArrayList<String>();
    static int dp=0;
    static int wps=0;
    static Timer t;
    static JLabel whitePawn = new JLabel();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                    try {
                        Chessboard window = new Chessboard();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                        e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     * @throws IOException 
     */
    public Chessboard() throws IOException {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     * @throws IOException 
     */
    private void initialize() throws IOException {
        addFileLoc();
        frame = new JFrame();

        frame.getContentPane().setBackground(Color.PINK);
        frame.setBounds(0, 0, 551, 440);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setResizable(false);

        JPanel panel_2 = new JPanel();
        panel_2.setBounds(401, 0, 150, 411);
        frame.getContentPane().add(panel_2);

        // WHITE PAWN  C:\Users\USER\eclipse-workspace\CookieClicker\Images\whitepawn.png
        Icon icon;
        icon = new ImageIcon("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\12.png");

        for (int wp=0; wp<=8*50; wp+=50) {
            whitePawn.setIcon(icon);
            whitePawn.setBounds(wp,50,50,50);
            frame.getContentPane().add(whitePawn);
        }

        // BLACK PAWN

        icon = new ImageIcon("C:\\\\Users\\\\USER\\\\eclipse-workspace\\\\CookieClicker\\\\chess images\\\\White Pawn.png");
        for (int dp=0; dp<=8*50;dp+=50) {
            JLabel blackPawn = new JLabel(icon);
            blackPawn.setBounds(0+dp, 300, 50, 50);
            frame.getContentPane().add(blackPawn);

        }

        for (dp=0; dp<=7;dp++) {
            for (int i = 0; i<=8*50;i+=50) {    

                    icon = new ImageIcon(blackFileLoc.get(dp));
                JLabel blackPawn = new JLabel(icon);
                blackPawn.setBounds(0+i, 0, 50, 50);
                if (dp<=6) {
                    dp+=1;
                }
                frame.getContentPane().add(blackPawn);

            }
        }    

        // white pieces
        for (wps=0; wps<=7;wps++) {
            for (int i = 0; i<=8*50;i+=50) {    

                icon = new ImageIcon(whiteFileLoc.get(wps));
                JLabel blackPawn = new JLabel(icon);
                blackPawn.setBounds(0+i, 350, 50, 50);
                if (wps<=6) {
                    wps+=1;
                }
                frame.getContentPane().add(blackPawn);

            }
        }

        for (int y = 0; y<=3*100;y+=100) {
            for (int x = 0; x<=3*100;x+=100) {
                JPanel panel = new JPanel();
                panel.setBackground(Color.PINK);
                panel.setBounds(0+x, 0+y, 50, 50);
                frame.getContentPane().add(panel);
            }

            for (int i=0; i<=6*50;i+=50) {
                    JPanel panel_1 = new JPanel();
                    panel_1.setBackground(Color.GRAY);
                panel_1.setBounds(50+i, 0+y, 50, 50);
                frame.getContentPane().add(panel_1);
            }
        }

        for (int y = 0; y<=3*100;y+=100) {
            for (int x = 0; x<=3*100;x+=100) {
                JPanel panel_1 = new JPanel();
                panel_1.setBackground(Color.GRAY);
                panel_1.setBounds(0+x, 50+y, 50, 50);
                frame.getContentPane().add(panel_1);
            }

            for (int i=0; i<=16*100;i+=100) {
                JPanel panel = new JPanel();
                panel.setBackground(Color.PINK);
                panel.setBounds(50+y, 50+y, 50, 50);
                frame.getContentPane().add(panel);
            }

        }   

        whitePawn.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                System.out.println("test");
                PointerInfo a = MouseInfo.getPointerInfo();
                Point b = a.getLocation();
                int x = (int) b.getX();
                int y = (int) b.getY();
                whitePawn.setBounds(x-50,y-50,50,50);
            }
        });

        t = new Timer(1,(ActionEvent e)->{
            whitePawn.setBounds(whitePawn.getX(),whitePawn.getY(),whitePawn.getWidth(),whitePawn.getHeight());
        });
        t.start();
    }

    public void addFileLoc() {
        // first step

        // second step

        blackFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\Black Rhook.png");
        blackFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\Black Knight.png");
        blackFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\Black Bishop.png");
        blackFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\Black King.png");
        blackFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\Black Queen.png");

        //this reverse the file loc
        for (int i=2;i>=0;i--) {
                blackFileLoc.add(blackFileLoc.get(i));
        }

        whiteFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\White Rhook.png");
        whiteFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\White Knight.png");
        whiteFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\White Bishop.png");
        whiteFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\White Queen.png");
        whiteFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\White King.png");

        //this reverse the file loc
        for (int i=2;i>=0;i--) {
            whiteFileLoc.add(whiteFileLoc.get(i));
        }

    }

}

现在,如果我们添加鼠标运动监听器,这是代码图标将不会出现黑色棋子,有人能给我一个方法,使它的碎片将被拖动?

qlfbtfca

qlfbtfca1#

好的,在一些测试之后,我意识到你不能把鼠标拖到for循环之外,如果你把它放到for循环中,就像下面的代码:

for (int dp=0; dp<=7*50;dp+=50) {
        JLabel blackPawn = new JLabel(icon);
        blackPawn.setBounds(0+dp, 300, 50, 50);
        frame.getContentPane().add(blackPawn);

        blackPawn.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                PointerInfo a = MouseInfo.getPointerInfo();
                Point b = a.getLocation();
                int x = (int) b.getX();
                int y = (int) b.getY();
                blackPawn.setBounds(x-32, y-50, 50, 50);
            }
        });
    }

它使每一个单一的功能没有使任何图标消失或任何形式的错误。

相关问题