8.3 鼠标事件
鼠标按下:mousePressed
鼠标释放:mouseReleased
鼠标点击:mouseClicked
点击次数:getClickCount===>可用于区分单击双击三击
鼠标光标形状:
未按住鼠标是箭头,按住鼠标是十字,可通过 MouseEvent 的 getPoint() 确定是否按住鼠标,getPoint()不为空时按住鼠标,否则未按住。
原书例子:
绘制正方形
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
Main solution = new Main();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MouseFrame b = new MouseFrame();
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setVisible(true);
}
});
}
}
class MouseFrame extends JFrame{
public static final int W = 300;
public static final int H = 200;
public MouseFrame(){
setTitle("MouseTest");
setSize(W,H);
MouseComponent component = new MouseComponent();
add(component);
}
}
class MouseComponent extends JComponent{
private static final int SIDELENGTH = 10;
private ArrayList<Rectangle2D> squares;
private Rectangle2D current;
public MouseComponent(){
squares = new ArrayList<>();
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
for(Rectangle2D r:squares){
g2.draw(r);
}
}
public void add(Point2D p){
double x = p.getX();
double y = p.getY();
current = new Rectangle2D.Double(x-SIDELENGTH/2,y-SIDELENGTH/2,SIDELENGTH,SIDELENGTH);
squares.add(current);
repaint();
}
public void remove(Rectangle2D s){
if(s == null) return;
if(s == current) current = null;
squares.remove(s);
repaint();
}
public Rectangle2D find(Point2D p){
for(Rectangle2D r:squares){
if(r.contains(p))
return r;
}
return null;
}
private class MouseHandler extends MouseAdapter{
public void mousePressed(MouseEvent event){
current = find(event.getPoint());
if(current == null)
add(event.getPoint());
}
@Override
public void mouseClicked(MouseEvent event) {
current = find(event.getPoint());
if(current!=null && event.getClickCount() >= 2)
remove(current);
}
}
private class MouseMotionHandler implements MouseMotionListener{
@Override
public void mouseMoved(MouseEvent event) {
if(find(event.getPoint())==null){
setCursor(Cursor.getDefaultCursor());
}else{
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
}
@Override
public void mouseDragged(MouseEvent event) {
if(current != null){
//点击立刻拖拽改光标
if(getCursor()==Cursor.getDefaultCursor())
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
int x = event.getX();
int y = event.getY();
current.setFrame(x-SIDELENGTH/2,y-SIDELENGTH/2, SIDELENGTH, SIDELENGTH);
repaint();
}
}
}
}
8.4 AWT事件继承层次
两次 shift 找到AWTEvent类,点进去把光标移动到 AWTEvent 上,按 F4
AWT将事件分为:底层事件和表意事件
PS:Semantic 按照柯林斯的 按意思进行处理,中文可以说是表意
底层事件:按下鼠标,移动鼠标,抬起鼠标,拖动鼠标
表意事件:点击按钮,拖动滚动条
常用表意事件:
ActionEvent(选择,比如按钮点击、菜单列表项选择、文本框中回车)
AdjustmentEvent(滚动条事件)
ItemEvent(复选框或列表中选一项)
常用底层事件:
KeyEvent(键盘按键按下释放)
MouseEvent(鼠标键按下、释放、移动或拖动)
MouseWhellEvent(鼠标滚轮转动)
FocusEvent(组件获得焦点、失去焦点)
WindowEvent(窗体放大缩小、关闭【个人觉得这个应该算表意,因为放大缩小这个操作其实是点击了窗体对应按钮或位置实现的,底层对应于鼠标或键盘操作】)
常用适配器类:
FocusAdapter KeyAdapter MouseAdapter MouseMotionAdapter WindowAdapter
监听器具体有什么方法就不赘述了,因为这些东西大家都不会很刻意的去记忆,通常记得就用,不记得就查一下,监听器或适配器对应的方法描述非常清楚明白,通常看一下方法名,大概就知道哪个了,不必要背下来。
相关内容:选择 《Java核心技术 卷1》查找相关笔记
喜欢的话,点个赞吧~!平时做题,以及笔记内容将更新到公众号。
关注公众号,互相学习:钰娘娘知识汇总
【文章原创作者:武汉网站建设 http://www.wh5w.com欢迎留下您的宝贵建议】