學習筆記二十六:事件處理(二) - 新聞資訊 - 雲南小程序開發|雲南軟件開發|雲南網站建設-昆明融晨信息技術有限公司

159-8711-8523

雲南網建設/小程序開發/軟件開發

知識

不(bù)管是(shì)網站,軟件還是(shì)小程序,都要(yào / yāo)直接或間接能爲(wéi / wèi)您産生價值,我們在(zài)追求其視覺表現的(de)同時(shí),更側重于(yú)功能的(de)便捷,營銷的(de)便利,運營的(de)高效,讓網站成爲(wéi / wèi)營銷工具,讓軟件能切實提升企業内部管理水平和(hé / huò)效率。優秀的(de)程序爲(wéi / wèi)後期升級提供便捷的(de)支持!

您當前位置>首頁 » 新聞資訊 » 技術分享 >

學習筆記二十六:事件處理(二)

發表時(shí)間:2020-11-13

發布人(rén):融晨科技

浏覽次數:60


在(zài)這(zhè)個(gè)并非盡善盡美的(de)世界上(shàng),勤奮會得到(dào)報償,而(ér)遊手好閑則要(yào / yāo)受到(dào)懲罰。——毛姆


本講内容:MouseEvent 、MouseMotionListener


一、MouseEvent   讓鼠标能知道(dào)鼠标按下的(de)消息、知道(dào)點擊的(de)位置等五個(gè)方法。
MouseMotionListener鼠标拖動坐标、鼠标移動坐标二個(gè)方法。



public class Text extends JFrame {
	MyPanel mb=null;
	
	public static void main(String[] args) {
		Text t=new Text();//每定義一個(gè) t 都會産生一個(gè)對應的(de)this
	}
	
	public Text() {
		mb=new MyPanel();
		this.addMouseListener(mb);//添加監聽   
		this.addMouseMotionListener(mb);
		this.add(mb);
		
		//設置窗體屬性
		this.setTitle("技術大(dà)牛—小勁");
		this.setLocation(300, 300);
		this.setSize(400,400);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

class MyPanel extends JPanel implements MouseListener,MouseMotionListener{
	public void paint(Graphics g) {
		super.paint(g);
	}

	//MouseListener  接口方法
	//鼠标被點擊(鼠标按下然後松開後執行)
	public void mouseClicked(MouseEvent e) {
		System.out.println("鼠标點擊了(le/liǎo) x="+e.getX()+"y="+e.getY());
	}

	//鼠标移動到(dào)MyPanel
	public void mouseEntered(MouseEvent e) {
		System.out.println("鼠标移動到(dào)MyPanel");
	}

	//鼠标離開MyPanel
	public void mouseExited(MouseEvent e) {
		System.out.println("鼠标離開MyPanel");
	}

	//鼠标按下
	public void mousePressed(MouseEvent e) {
		System.out.println("鼠标按下");
	}

	//鼠标松開
	public void mouseReleased(MouseEvent e) {
		System.out.println("鼠标松開");
	}

	
	//MouseMotionListener  接口方法
	//鼠标拖動
	public void mouseDragged(MouseEvent e) {
		System.out.println("鼠标拖拽的(de)當前坐标  x="+e.getX()+"y="+e.getY());
	}

	//鼠标移動
	public void mouseMoved(MouseEvent e) {
		System.out.println("鼠标當前坐标  x="+e.getX()+"y="+e.getY());		
	}
}

二、KeyListener 鍵盤事件有三個(gè)方法。WindowListener 窗口事件有七個(gè)方法。
public class Text extends JFrame {
	MyPanel mb=null;
	
	public static void main(String[] args) {
		Text t=new Text();
	}
	
	public Text() {
		mb=new MyPanel();
		this.addKeyListener(mb);
		this.addWindowListener(mb);
		this.add(mb);
		
		//設置窗體屬性
		this.setTitle("技術大(dà)牛—小勁");
		this.setLocation(300, 300);
		this.setSize(400,400);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

class MyPanel extends JPanel implements KeyListener,WindowListener{
	public void paint(Graphics g) {
		super.paint(g);
	}

	//鍵被按下  
	public void keyPressed(KeyEvent e) {
		System.out.println(e.getKeyChar()+"鍵被按下了(le/liǎo)");//e.getKeyCode()是(shì)ASCII碼
	}

	//鍵被釋放  
	public void keyReleased(KeyEvent e) {
		
	}

	//鍵的(de)一個(gè)值被打印輸出(chū)(上(shàng)下左右鍵等不(bù)會執行,因爲(wéi / wèi)沒打印出(chū))
	public void keyTyped(KeyEvent e) {
		
	}

	
	//窗口激活了(le/liǎo)(窗口還原後即激活)
	public void windowActivated(WindowEvent e) {
		System.out.println("窗口激活了(le/liǎo)");
	}

	//窗口關閉
	public void windowClosed(WindowEvent e) {//很少用到(dào)
		System.out.println("窗口關閉");
	}

	//窗口正在(zài)關閉
	public void windowClosing(WindowEvent e) {
		System.out.println("窗口正在(zài)關閉");
	}

	//窗口失去激活(窗口最小化)
	public void windowDeactivated(WindowEvent e) {
		System.out.println("窗口失去激活");
	}

	//窗口還原
	public void windowDeiconified(WindowEvent e) {
		System.out.println("窗口還原");
	}

	//窗口最小化
	public void windowIconified(WindowEvent e) {
		System.out.println("窗口最小化");
	}

	 //窗口打開
	public void windowOpened(WindowEvent e) {
		System.out.println("窗口打開");	
	}
}



本講就(jiù)到(dào)這(zhè)裏,Take your time and enjoy it

相關案例查看更多