java界面相关
我新建一个窗口,然后在窗口里面添加了一个JPanel,然后实现可以在这个panel中单击鼠标拖动时,窗口随着变动,请各位高手指点!!!...
我新建一个窗口,然后在窗口里面添加了一个JPanel,然后实现可以在这个panel中单击鼠标拖动时,窗口随着变动,请各位高手指点!!!
展开
展开全部
//要注册两个监听器,MouseListener,MouseMotionListener,
//以下代码适用JDK1.5以上,如果你是JDK1.6以上,
//MouseEvent e,e.getXOnScreen();将可用,这个就更方便,
//如果你是JDK1.5以前,则更麻烦一点
//:MouseDragTest.java
import java.awt.event.*;
import javax.swing.*;
public class MouseDragTest {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setUndecorated(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300,200);
f.setLocationRelativeTo(null);
f.setContentPane(new MyPanel2());
f.setVisible(true);
}
}
class MyPanel2 extends JPanel implements MouseListener,MouseMotionListener{
private int x,y;
private java.awt.Window root;
MyPanel2(){
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setBackground(java.awt.Color.pink);
this.add(new JLabel("CTRL+单击 或双击 关闭窗口"));
}
//CTRL+单击 或双击 关闭窗口
public void mouseClicked(MouseEvent e) {
if(e.isControlDown()||e.getClickCount()==2)
root.dispose();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
//鼠标按下时,先计算鼠标当前位置与窗口左上角的距离 x,y
public void mousePressed(MouseEvent e) {
if(root==null){
java.awt.Container parent=null;
for(parent=this.getParent();parent!=null;parent=parent.getParent())
if(parent instanceof java.awt.Window){
root = (java.awt.Window)parent;
break;
}
}
if(root==null)return;
java.awt.Point p=java.awt.MouseInfo.getPointerInfo().getLocation();
x=p.x-root.getX();
y=p.y-root.getY();
}
public void mouseReleased(MouseEvent e) {}
//鼠标拖动时使窗口左上角的位置和鼠标当前距离始终保持在x,y
public void mouseDragged(MouseEvent e) {
if(root==null)return;
java.awt.Point p=java.awt.MouseInfo.getPointerInfo().getLocation();
root.setLocation(p.x-x,p.y-y);
}
public void mouseMoved(MouseEvent e) {}
}
//以下代码适用JDK1.5以上,如果你是JDK1.6以上,
//MouseEvent e,e.getXOnScreen();将可用,这个就更方便,
//如果你是JDK1.5以前,则更麻烦一点
//:MouseDragTest.java
import java.awt.event.*;
import javax.swing.*;
public class MouseDragTest {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setUndecorated(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300,200);
f.setLocationRelativeTo(null);
f.setContentPane(new MyPanel2());
f.setVisible(true);
}
}
class MyPanel2 extends JPanel implements MouseListener,MouseMotionListener{
private int x,y;
private java.awt.Window root;
MyPanel2(){
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setBackground(java.awt.Color.pink);
this.add(new JLabel("CTRL+单击 或双击 关闭窗口"));
}
//CTRL+单击 或双击 关闭窗口
public void mouseClicked(MouseEvent e) {
if(e.isControlDown()||e.getClickCount()==2)
root.dispose();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
//鼠标按下时,先计算鼠标当前位置与窗口左上角的距离 x,y
public void mousePressed(MouseEvent e) {
if(root==null){
java.awt.Container parent=null;
for(parent=this.getParent();parent!=null;parent=parent.getParent())
if(parent instanceof java.awt.Window){
root = (java.awt.Window)parent;
break;
}
}
if(root==null)return;
java.awt.Point p=java.awt.MouseInfo.getPointerInfo().getLocation();
x=p.x-root.getX();
y=p.y-root.getY();
}
public void mouseReleased(MouseEvent e) {}
//鼠标拖动时使窗口左上角的位置和鼠标当前距离始终保持在x,y
public void mouseDragged(MouseEvent e) {
if(root==null)return;
java.awt.Point p=java.awt.MouseInfo.getPointerInfo().getLocation();
root.setLocation(p.x-x,p.y-y);
}
public void mouseMoved(MouseEvent e) {}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询