他们是使 JLabel 可移动的方法吗?
Is their a way to make JLabels moveable?
我正在尝试制作一个带有可拖动棋子的棋盘,我添加了一个鼠标运动监听器,每次拖动时,棋子都不会移动。例如,如果我将我的白色棋子拖到位置 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));
}
}
}
现在如果我们添加鼠标移动侦听器,这是图标不会出现黑色 pawn 的代码,有人可以给我一个方法让棋子可以拖动吗?
好的,经过一些测试,我意识到你不能将鼠标拖动到 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);
}
});
}
它使每一件作品都能发挥作用,而不会使任何图标消失或出现任何类型的错误。
我正在尝试制作一个带有可拖动棋子的棋盘,我添加了一个鼠标运动监听器,每次拖动时,棋子都不会移动。例如,如果我将我的白色棋子拖到位置 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));
}
}
}
现在如果我们添加鼠标移动侦听器,这是图标不会出现黑色 pawn 的代码,有人可以给我一个方法让棋子可以拖动吗?
好的,经过一些测试,我意识到你不能将鼠标拖动到 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);
}
});
}
它使每一件作品都能发挥作用,而不会使任何图标消失或出现任何类型的错误。