java se中 怎么让鼠标点下窗口中的任何地方都能实现拖动窗口标题栏一样的效果 具体代码?求高手指导
1个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KillingTwoStonesWithOneBird {
public KillingTwoStonesWithOneBird() {
JFrame f = new JFrame();
MyMouseAdapter adapter = new MyMouseAdapter();
f.addMouseMotionListener(adapter);
f.addMouseListener(adapter);
f.setAlwaysOnTop(true);
f.setSize(500, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
class MyMouseAdapter extends MouseAdapter {
private int offsetX, offsetY;
public void mouseDragged(MouseEvent e) {
// requires JDK 1.6 or above
SwingUtilities.getRoot((Component) e.getSource()).setLocation(e.getXOnScreen() - offsetX, e.getYOnScreen()- offsetY);
}
public void mousePressed(MouseEvent e) {
offsetX = e.getX();
offsetY = e.getY();
}
}
public static void main(String[] args) {
new KillingTwoStonesWithOneBird();
}
}
import java.awt.event.*;
import javax.swing.*;
public class KillingTwoStonesWithOneBird {
public KillingTwoStonesWithOneBird() {
JFrame f = new JFrame();
MyMouseAdapter adapter = new MyMouseAdapter();
f.addMouseMotionListener(adapter);
f.addMouseListener(adapter);
f.setAlwaysOnTop(true);
f.setSize(500, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
class MyMouseAdapter extends MouseAdapter {
private int offsetX, offsetY;
public void mouseDragged(MouseEvent e) {
// requires JDK 1.6 or above
SwingUtilities.getRoot((Component) e.getSource()).setLocation(e.getXOnScreen() - offsetX, e.getYOnScreen()- offsetY);
}
public void mousePressed(MouseEvent e) {
offsetX = e.getX();
offsetY = e.getY();
}
}
public static void main(String[] args) {
new KillingTwoStonesWithOneBird();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询