知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
學(xué)習(xí)筆記二十六:事件處理(二)
發(fā)表時(shí)間:2020-11-13
發(fā)布人:葵宇科技
瀏覽次數(shù):60
在這個(gè)并非盡善盡美的世界上,勤奮會(huì)得到報(bào)償,而游手好閑則要受到懲罰。——毛姆
本講內(nèi)容:MouseEvent 、MouseMotionListener
一、MouseEvent 讓鼠標(biāo)能知道鼠標(biāo)按下的消息、知道點(diǎn)擊的位置等五個(gè)方法。
MouseMotionListener鼠標(biāo)拖動(dòng)坐標(biāo)、鼠標(biāo)移動(dòng)坐標(biāo)二個(gè)方法。
public class Text extends JFrame { MyPanel mb=null; public static void main(String[] args) { Text t=new Text();//每定義一個(gè) t 都會(huì)產(chǎn)生一個(gè)對(duì)應(yīng)的this } public Text() { mb=new MyPanel(); this.addMouseListener(mb);//添加監(jiān)聽 this.addMouseMotionListener(mb); this.add(mb); //設(shè)置窗體屬性 this.setTitle("技術(shù)大牛—小勁"); 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 接口方法 //鼠標(biāo)被點(diǎn)擊(鼠標(biāo)按下然后松開后執(zhí)行) public void mouseClicked(MouseEvent e) { System.out.println("鼠標(biāo)點(diǎn)擊了 x="+e.getX()+"y="+e.getY()); } //鼠標(biāo)移動(dòng)到MyPanel public void mouseEntered(MouseEvent e) { System.out.println("鼠標(biāo)移動(dòng)到MyPanel"); } //鼠標(biāo)離開MyPanel public void mouseExited(MouseEvent e) { System.out.println("鼠標(biāo)離開MyPanel"); } //鼠標(biāo)按下 public void mousePressed(MouseEvent e) { System.out.println("鼠標(biāo)按下"); } //鼠標(biāo)松開 public void mouseReleased(MouseEvent e) { System.out.println("鼠標(biāo)松開"); } //MouseMotionListener 接口方法 //鼠標(biāo)拖動(dòng) public void mouseDragged(MouseEvent e) { System.out.println("鼠標(biāo)拖拽的當(dāng)前坐標(biāo) x="+e.getX()+"y="+e.getY()); } //鼠標(biāo)移動(dòng) public void mouseMoved(MouseEvent e) { System.out.println("鼠標(biāo)當(dāng)前坐標(biāo) 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); //設(shè)置窗體屬性 this.setTitle("技術(shù)大?!?quot;); 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()+"鍵被按下了");//e.getKeyCode()是ASCII碼 } //鍵被釋放 public void keyReleased(KeyEvent e) { } //鍵的一個(gè)值被打印輸出(上下左右鍵等不會(huì)執(zhí)行,因?yàn)闆]打印出) public void keyTyped(KeyEvent e) { } //窗口激活了(窗口還原后即激活) public void windowActivated(WindowEvent e) { System.out.println("窗口激活了"); } //窗口關(guān)閉 public void windowClosed(WindowEvent e) {//很少用到 System.out.println("窗口關(guān)閉"); } //窗口正在關(guān)閉 public void windowClosing(WindowEvent e) { System.out.println("窗口正在關(guān)閉"); } //窗口失去激活(窗口最小化) 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("窗口打開"); } }
本講就到這里,Take your time and enjoy it
相關(guān)案例查看更多
相關(guān)閱讀
- 麗江小程序開發(fā)
- 汽車報(bào)廢回收
- 網(wǎng)站建設(shè)方案 doc
- 云南網(wǎng)站開發(fā)
- 云南小程序開發(fā)首選品牌
- 網(wǎng)站開發(fā)
- 定制小程序開發(fā)
- 云南網(wǎng)站建設(shè)服務(wù)公司
- 制作一個(gè)小程序
- web教程
- 云南網(wǎng)站優(yōu)化公司
- 百度小程序公司
- 網(wǎng)站制作哪家好
- 網(wǎng)站建設(shè)公司網(wǎng)站
- 企業(yè)網(wǎng)站
- 怎么做網(wǎng)站
- 北京小程序開發(fā)
- 英文網(wǎng)站建設(shè)公司
- 云南小程序開發(fā)哪家好
- 云南小程序商城
- 小程序商城
- 云南網(wǎng)絡(luò)公司
- 網(wǎng)站建設(shè)費(fèi)用
- 昆明軟件定制公司
- 二叉樹
- 汽車拆解管理系統(tǒng)
- 云南軟件設(shè)計(jì)
- 網(wǎng)站排名
- 人口普查小程序
- 網(wǎng)站建設(shè)報(bào)價(jià)