JAVA SWING 无边框(无TITLE)窗体如何实现窗体移动
如题,当鼠标在某一区域(比如一个LABEL上)按下(一个mousedowm事件),然后拖动鼠标,能使当前窗体移动。就和鼠标在TITLE上按下并且拖动时一样的效果。...
如题,当鼠标在某一区域(比如一个LABEL上)按下(一个mousedowm事件),然后拖动鼠标,能使当前窗体移动。就和鼠标在TITLE上按下并且拖动时一样的效果。
展开
展开全部
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
public class test extends JFrame{
private int xx, yy;
private boolean isDraging = false;
public test(){
setUndecorated(true); // 没有标题栏
setSize(200, 200);
setVisible(true);
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
isDraging = true;
xx = e.getX();
yy = e.getY();
}
public void mouseReleased(MouseEvent e) {
isDraging = false;
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if (isDraging) {
int left = getLocation().x;
int top = getLocation().y;
setLocation(left + e.getX() - xx, top + e.getY() - yy);
}
}
});
}
public static void main(String[] args) {
test t =new test();
t.setDefaultCloseOperation(3);
}
}
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
public class test extends JFrame{
private int xx, yy;
private boolean isDraging = false;
public test(){
setUndecorated(true); // 没有标题栏
setSize(200, 200);
setVisible(true);
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
isDraging = true;
xx = e.getX();
yy = e.getY();
}
public void mouseReleased(MouseEvent e) {
isDraging = false;
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if (isDraging) {
int left = getLocation().x;
int top = getLocation().y;
setLocation(left + e.getX() - xx, top + e.getY() - yy);
}
}
});
}
public static void main(String[] args) {
test t =new test();
t.setDefaultCloseOperation(3);
}
}
展开全部
只能给你个思路,只要能确定鼠标指针在屏幕中的坐标,以此坐标,来重置Frame的坐标,需要动态的获取,重置,用线程实现--个人思路
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
对了 你无边框 无title 是如何实现的 麻烦能告诉我一下吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询