我如何让标签(图片)“可拖拽”?所以,基本上是为了制作它,这样我就可以单击jlabel图片,并在jframe周围移动它并将其放入其中。我到处寻找,但对我没有任何帮助,所以我想知道你们中是否有人知道如何让它工作?
package src;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
/*
* @Author - 0x29A
*
*
*/
public class Jframe {
public static void main(final String args[]) {
/*
* @Images
*/
final ImageIcon icon = new ImageIcon("Data/button.png");
final JLabel label = new JLabel(icon);
final ImageIcon icon1 = new ImageIcon("Data/button1.png");
final JLabel label1 = new JLabel(icon1);
final ImageIcon icon2 = new ImageIcon("Data/button2.png");
final JLabel label2 = new JLabel(icon2);
final ImageIcon icon3 = new ImageIcon("Data/button3.png");
final JLabel label3 = new JLabel(icon3);
final ImageIcon icon4 = new ImageIcon("Data/button4.png");
final JLabel label4 = new JLabel(icon4);
final ImageIcon icon5 = new ImageIcon("Data/button5.png");
final JLabel label5 = new JLabel(icon5);
final ImageIcon icon6 = new ImageIcon("Data/background.png");
final JLabel label6 = new JLabel(icon6);
/*
* @Image Location
*/
label.setBounds(282, 255, 96, 96);
label1.setBounds(384, 255, 96, 96);
label2.setBounds(282, 153, 96, 96);
label3.setBounds(384, 153, 198, 96);
label4.setBounds(181, 152, 96, 96);
label5.setBounds(181, 255, 96, 96);
label6.setBounds(0, 0, 765, 503);
/*
* @Frame
*/
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(765, 503));
frame.setLayout(null);
frame.add(label);
frame.add(label1);
frame.add(label2);
frame.add(label3);
frame.add(label4);
frame.add(label5);
frame.add(label6);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}发布于 2012-07-24 07:44:00
归根结底,它有许多要求。主要的问题是“谁负责管理”。您可以实现它,以便每个标签都负责它自己的布局管理,或者让容器负责。
每种方法都有各自的优点。我更喜欢容器管理的方法,因为它不会限制你只能使用标签。
您需要熟悉一下MouseMotionListener和MouseListener。
基本思想是监视何时发生单击(以及单击了什么),并使用单击点确定鼠标被拖动时的偏移量。
更新
抱歉,我花了一些时间才找到,但我回答了一个可能对Object on Image does not move when Image moves有用的问题。
它并不完全相同,但它包含的概念可能会有所帮助。
https://stackoverflow.com/questions/11621501
复制相似问题