当前位置: 技术问答>java相关
关于JLabel的移动问题。
来源: 互联网 发布时间:2015-08-08
本文导语: 我是这样做的, import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class Frame1 extends Frame { public JLabel bl = new JLabel(" b1 "); public Frame1() { this.setLayout(null); ...
我是这样做的,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Frame1 extends Frame {
public JLabel bl = new JLabel(" b1 ");
public Frame1() {
this.setLayout(null);
bl.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
System.out.println("dkfj");
JLabel ls = (JLabel) e.getComponent();
ls.setLocation(ls.getX()+e.getX(),ls.getY()+e.getY());
}
});
bl.setBounds(new Rectangle(100, 39, 55, 22));
bl.setBorder(new LineBorder(Color.BLUE, 1));
this.add(bl);
}
public static void main(String[] args) {
Frame1 frame = new Frame1();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(300, 400);
frame.setVisible(true);
}
}
但感觉不好,因为不能保持鼠标在JLabel中的相对位置,开始移动后,鼠标
总是跑到相对JLabel的0,0位置。
问:该如何解决?
先谢谢!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Frame1 extends Frame {
public JLabel bl = new JLabel(" b1 ");
public Frame1() {
this.setLayout(null);
bl.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
System.out.println("dkfj");
JLabel ls = (JLabel) e.getComponent();
ls.setLocation(ls.getX()+e.getX(),ls.getY()+e.getY());
}
});
bl.setBounds(new Rectangle(100, 39, 55, 22));
bl.setBorder(new LineBorder(Color.BLUE, 1));
this.add(bl);
}
public static void main(String[] args) {
Frame1 frame = new Frame1();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(300, 400);
frame.setVisible(true);
}
}
但感觉不好,因为不能保持鼠标在JLabel中的相对位置,开始移动后,鼠标
总是跑到相对JLabel的0,0位置。
问:该如何解决?
先谢谢!
|
public JLabel bl = new JLabel(" b1 ");
private Point lastLocation = null;
public Frame1() {
this.setLayout(null);
bl.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if( lastLocation == null )
return;
JLabel ls = (JLabel) e.getComponent();
int incx = lastLocation.x - e.getX(),
incy = lastLocation.y - e.getY();
ls.setLocation(ls.getX()-incx,ls.getY()-incy);
}
});
bl.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e ) {
lastLocation = e.getPoint();
}
public void mouseReleased(MouseEvent e ) {
lastLocation = null;
}
});
bl.setBounds(new Rectangle(100, 39, 55, 22));
bl.setBorder(new LineBorder(Color.blue, 1));
this.add(bl);
}
private Point lastLocation = null;
public Frame1() {
this.setLayout(null);
bl.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if( lastLocation == null )
return;
JLabel ls = (JLabel) e.getComponent();
int incx = lastLocation.x - e.getX(),
incy = lastLocation.y - e.getY();
ls.setLocation(ls.getX()-incx,ls.getY()-incy);
}
});
bl.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e ) {
lastLocation = e.getPoint();
}
public void mouseReleased(MouseEvent e ) {
lastLocation = null;
}
});
bl.setBounds(new Rectangle(100, 39, 55, 22));
bl.setBorder(new LineBorder(Color.blue, 1));
this.add(bl);
}
|
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Frame1 extends Frame {
public JLabel bl = new JLabel(" b1 ");
int startX,startY;
public Frame1() {
this.setLayout(null);
bl.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
System.out.println("dkfj");
JLabel ls = (JLabel) e.getComponent();
ls.setLocation(ls.getX()+e.getX()- startX,ls.getY()+e.getY()- startY);
}
});
bl.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(MouseEvent e) {
JLabel ls = (JLabel) e.getComponent();
startX = e.getX();
startY = e.getY();
}
});
bl.setBounds(new Rectangle(100, 39, 55, 22));
bl.setBorder(new LineBorder(Color.BLUE, 1));
this.add(bl);
}
public static void main(String[] args) {
Frame1 frame = new Frame1();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(300, 400);
frame.setVisible(true);
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Frame1 extends Frame {
public JLabel bl = new JLabel(" b1 ");
int startX,startY;
public Frame1() {
this.setLayout(null);
bl.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
System.out.println("dkfj");
JLabel ls = (JLabel) e.getComponent();
ls.setLocation(ls.getX()+e.getX()- startX,ls.getY()+e.getY()- startY);
}
});
bl.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(MouseEvent e) {
JLabel ls = (JLabel) e.getComponent();
startX = e.getX();
startY = e.getY();
}
});
bl.setBounds(new Rectangle(100, 39, 55, 22));
bl.setBorder(new LineBorder(Color.BLUE, 1));
this.add(bl);
}
public static void main(String[] args) {
Frame1 frame = new Frame1();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(300, 400);
frame.setVisible(true);
}
}