📄 mouse.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MoveRect extends Applet{
boolean move = false; // 标志是否正在移动
int dx = 10; // 方块的基本位置
int dy = 10;
int oldx ; //存放单击时候和现在位置的差
int oldy ;
public void init(){}
public void paint(Graphics g){
g.drawRect(dx,dy,20,20);
}
void SetCursor(int x, int y) // 设置光标
{
Rectangle r=new Rectangle(dx,dy,20,20); // 取方框的范围
Frame tmpFrame;
Component ParentComponent;
ParentComponent = getParent();
while ( ParentComponent != null && !(ParentComponent instanceof Frame))
ParentComponent = ParentComponent.getParent();//找到最顶层的Frame容器
tmpFrame = (Frame) ParentComponent;
if(r.contains(x,y)) // 如果在方框范围内
tmpFrame.setCursor(Cursor.MOVE_CURSOR); // 改变光标为移动时候的光标
else
tmpFrame.setCursor(Cursor.DEFAULT_CURSOR);// 恢复为正常的光标
}
public boolean mouseMove(Event evt,int x,int y)
{
SetCursor(x,y); // 设置光标形状
return true;
}
public boolean mouseDown(Event evt,int x,int y)
{
Rectangle r=new Rectangle(dx,dy,dx+10,dy+10);
if(r.contains(x,y)) // 如果是在方框内按下鼠标
{
move = true; // 设置为正在移动标志
oldx=x-dx; //保存当前按下的位置与方框基础位置的差
oldy=y-dy;
}
else move = false;
return true; // 告诉系统已经处理了鼠标事件
}
public boolean mouseUp(Event evt,int x,int y)
{
move = false;//放开按钮后,移动结束
return true;
}
public boolean mouseDrag(Event evt,int x,int y)
{
if(move)//如果正在拖动方框
{
dx=x - oldx;//计算方框新位置
dy=y - oldy;
repaint();
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -