如何在JavaGUI中使用鼠标光标拖动图像?
//我的代码调用要放置在JPanel上的目录中的n个图像如何在JavaGUI中使用鼠标光标拖动图像?,java,image,user-interface,drag,imageicon,Java,Image,User Interface,Drag,Imageicon,//我的代码调用要放置在JPanel上的目录中的n个图像 public void imageAdder(int n, String name){ BufferedImage myPic = null; for (int i = 0; i <= n; i++){ try { myPic = ImageIO.read(new File("Images/" + name + i + ".jpg")); } catch (Excep
public void imageAdder(int n, String name){
BufferedImage myPic = null;
for (int i = 0; i <= n; i++){
try {
myPic = ImageIO.read(new File("Images/" + name + i + ".jpg"));
} catch (Exception e){
System.out.println("no file man cmon");
}
JLabel picLabel = new JLabel(new ImageIcon(myPic));
// picLabel.setBounds(mouseX, mouseY, 100, 50);
// picLabel.addMouseMotionListener(this);
// picLabel.addMouseListener(this);
canvas.add(picLabel);
}}
public void imageAdder(int n,字符串名){
BuffereImage myPic=null;
对于(int i=0;i有许多方法可以实现这一点…例如,您可以使用自定义绘制自己绘制各个图像。每次按下/拖动鼠标时,您都需要计算拖动了哪个图像
一个稍微简单的解决方案可能是使用JLayeredPane
并继续使用JLabel
s来渲染图像,然后可以使用MouseListener
和MouseMotionListener
来检测标签何时被按下和/或拖动,并相应地更新其位置
有关更多详细信息,请参阅和
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestDrag {
public static void main(String[] args) {
new TestDrag();
}
public TestDrag() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JLayeredPane {
public TestPane() {
File[] images = new File("C:\\hold\\thumbnails").listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
String name = pathname.getName().toLowerCase();
return name.endsWith(".png") ||
name.endsWith(".jpg") ||
name.endsWith(".bmp") ||
name.endsWith(".gif");
}
});
int x = 0;
int y = 0;
for (File imgFile : images) {
try {
BufferedImage img = ImageIO.read(imgFile);
JLabel label = new JLabel(new ImageIcon(img));
label.setSize(label.getPreferredSize());
label.setLocation(x, y);
MouseHandler mh = new MouseHandler();
label.addMouseListener(mh);
label.addMouseMotionListener(mh);
add(label);
x += 20;
y += 20;
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 800);
}
public class MouseHandler extends MouseAdapter {
private Point offset;
@Override
public void mousePressed(MouseEvent e) {
JLabel label = (JLabel) e.getComponent();
moveToFront(label);
offset = e.getPoint();
}
@Override
public void mouseDragged(MouseEvent e) {
int x = e.getPoint().x - offset.x;
int y = e.getPoint().y - offset.y;
Component component = e.getComponent();
Point location = component.getLocation();
location.x += x;
location.y += y;
component.setLocation(location);
}
}
}
}
查看Java Swing中的自定义类,可能是帮助您入门的有用链接:
嘿,谢谢你的回复!我对java和面向对象语言还是很陌生,所以我想花几天时间来理解你编写的代码,看看它是否可以在我的游戏中实现。我会很快让你知道它是如何运行的,非常感谢你的帮助!我很感激。这只是一种方法,是我能做到的最快捷、最简单的方法nk当然,希望有帮助。这可能看起来像一个愚蠢的问题,但每个类都应该在一个单独的文件中,对吗?你排列括号的方式让我认为它们应该在同一个文件中,因为类testdrag是代码中的最后一个括号。但是我被告知每个类都应该有自己的文件。请在可以的时候告诉我。我这是一个自包含的示例,因此它可以驻留在单个类中,但它不一定要驻留,因为我使用的是源事件,鼠标句柄不需要包含在测试窗格中。这些被称为内部类。您的代码工作得很好!非常感谢!如果我想在同一个目录,这会导致任何问题吗?我尝试过,但它无法识别“moveToFront”方法。你知道为什么会出现这种情况,因为moveToFront是内置的吗?(导入不应该是问题,我解释过)