怎样在Jpanel中拖动Jlabel控件? 怎样在Jpanel中拖动Jlabel控件? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.event.*;import javax.swing.*;import java.awt.*;public class DragLabel extends JPanel{ private JLabel label; private int mx,my; public DragLabel(){ label = new JLabel("drag me"); label.setBackground(Color.RED); add(label); this.addMouseMotionListener(new DragHandler()); } private class DragHandler implements MouseMotionListener{ public void mouseDragged(MouseEvent e){ int lx = label.getLocation().x; int ly = label.getLocation().y; Dimension d = label.getSize(); if(new Rectangle(lx,ly,(int)(lx+d.getWidth()),(int)(ly+d.getWidth())).contains(e.getPoint())){ label.setLocation(lx-(mx-e.getX()),ly-(my-e.getY())); mx = e.getX(); my = e.getY(); } } public void mouseMoved(MouseEvent e){ mx = e.getX(); my = e.getY(); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JFrame f = new JFrame(); f.getContentPane().setLayout(new BorderLayout()); f.getContentPane().add(new DragLabel(),BorderLayout.CENTER); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(400,300); f.setVisible(true); }} if(new Rectangle(lx,ly,(int)(d.getWidth()),(int)(d.getHeight())).contains(e.getPoint())){ 基于java的浏览器 为什么Dialog会产生两个? 菜鸟提问:启动垃圾回收时,是否还能创建对象。 请问 Swing中画梯形的方法是什么??? 在使用quartz框架的时候添加CronTrigger的时候无法确定第一次执行的时间 请大家介绍一下什么叫java和数据结构结合(做学生成绩记录表)!大分在后面! 用Jbuilder怎么开发一个可以传文件的ftp小程序? 如何让浏览器可以打开SWing的Applet 这里的null代表的是Object还是String? 空字段显示 在Java中,怎样把一个图片文件写入到mssql数据库中,怎样读出来显示? 一道小小的面试题
import javax.swing.*;
import java.awt.*;
public class DragLabel extends JPanel{
private JLabel label;
private int mx,my;
public DragLabel(){
label = new JLabel("drag me");
label.setBackground(Color.RED);
add(label);
this.addMouseMotionListener(new DragHandler());
}
private class DragHandler implements MouseMotionListener{
public void mouseDragged(MouseEvent e){
int lx = label.getLocation().x;
int ly = label.getLocation().y;
Dimension d = label.getSize();
if(new Rectangle(lx,ly,(int)(lx+d.getWidth()),(int)(ly+d.getWidth())).contains(e.getPoint())){
label.setLocation(lx-(mx-e.getX()),ly-(my-e.getY()));
mx = e.getX();
my = e.getY();
}
}
public void mouseMoved(MouseEvent e){
mx = e.getX();
my = e.getY();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame f = new JFrame();
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(new DragLabel(),BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,300);
f.setVisible(true);
}}