JavaSwing鼠标的操作

JavaSwing的鼠标操作函数

MouseListener的使用: 

MouseListener

mouseClicked(MouseEvent e)

mouseEntered(MouseEvent e)

mouseExited(MouseEvent e)

mousePressed(MouseEvent e)

mouseReleased(MouseEvent e) 

下面我给大家举例说明一下以上方法的操作。

package com;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * 
 * @author chenhailong
 *
 */
public class MouseDemo1 extends WindowAdapter implements MouseListener{
    JFrame f=null;
    JButton b1=null;
    JLabel label=null;
    
    public MouseDemo1(){
       f=new JFrame("MouseDemo1");
       Container contentPane=f.getContentPane();
       contentPane.setLayout(new GridLayout(2,1));
       b1=new JButton("按钮");
       label=new JLabel("起始状态,还没有鼠标事件",JLabel.CENTER);
       b1.addMouseListener(this);
       contentPane.add(label);
       contentPane.add(b1);
       f.pack();
       f.setVisible(true);
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       f.addWindowListener(this);
    }
    public void mousePressed(MouseEvent e){
        label.setText("你已经压下鼠标按钮");
    }
    public void mouseReleased(MouseEvent e){
        label.setText("你已经放开鼠标按钮");
    }
    public void mouseEntered(MouseEvent e){
        label.setText("鼠标光标进入按钮");
    }
    public void mouseExited(MouseEvent e){
        label.setText("鼠标光标离开按钮");
    }
    public void mouseClicked(MouseEvent e){
        label.setText("你已经按下按钮");
    }
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
    public static void main(String[] args){
       new MouseDemo1();	
    }
}

MouseMotionListener的使用: 下面是讨论MouseMotionListener的使用时机,它提供的下面的两个方法,可让你随时掌握鼠标的坐标,并处理拖曳鼠标 的操作。

MouseMotionListener

mouseDragged(MouseEvent e)

mouseMoved(MouseEvent e)

下面的范例让你知道鼠标在JFrame上的坐标,并拖曳出直线来。

package com;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * 
 * @author chenhailong
 *
 */
public class MouseDemo2 extends JFrame implements MouseListener,
		MouseMotionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 532664972197143944L;
	int flag;// flag=1代表Mouse Moved,flag=2代表Mouse Dragged
	int x = 0;
	int y = 0;
	int startx, starty, endx, endy;// 起始坐标与终点坐标
	public MouseDemo2() {
		Container contentPane = getContentPane();
		contentPane.addMouseListener(this);
		contentPane.addMouseMotionListener(this);
		setSize(300, 300);
		setVisible(true);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	/* 由mousePressed(),mouseReleased()取得示拖曳的开始与结束坐标 */
	public void mousePressed(MouseEvent e) {
		startx = e.getX();
		starty = e.getY();
	}

	public void mouseReleased(MouseEvent e) {
		endx = e.getX();
		endy = e.getY();
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mouseClicked(MouseEvent e) {
	}

	/* mouseMoved(),mouseDragged()取得鼠标移动的每一个坐标,并调用repaint()方法 */
	public void mouseMoved(MouseEvent e) {
		flag = 1;
		x = e.getX();
		y = e.getY();
		repaint();
	}

	public void mouseDragged(MouseEvent e) {
		flag = 2;
		x = e.getX();
		y = e.getY();
		repaint();
	}

	public void update(Graphics g) {
		g.setColor(this.getBackground());
		g.fillRect(0, 0, getWidth(), getHeight());
		paint(g);
	}

	public void paint(Graphics g) {
		g.setColor(Color.black);
		if (flag == 1) {
			g.clearRect(0, 0, 300, 300);
			g.drawString("鼠标坐标:(" + x + "," + y + ")",10,50);
			g.drawLine(startx, starty, endx, endy);
		}
		if (flag == 2) {
			g.clearRect(0, 0, 300, 300);
			g.drawString("拖曳鼠标价坐标:(" + x + "," + y + ")", 10, 50);
			g.drawLine(startx, starty, x, y);
		}
	}

	public static void main(String[] args) {
		new MouseDemo2();
	}
}

 哈哈哦,好了大家尝试一下,就知道他的妙用了。。

你可能感兴趣的:(swing,F#)