欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java鼠标事件监听示例,java监听示例,package cn.o

来源: javaer 分享于  点击 35510 次 点评:5

java鼠标事件监听示例,java监听示例,package cn.o


package cn.outofmemory.snippets.desktop;import java.awt.BorderLayout;import java.awt.Component;import java.awt.Frame;import java.awt.TextArea;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionAdapter;public class MouseMotionListener {  public static void main(String[] args) {      // Create frame with specific title      Frame frame = new Frame("Example Frame");      // Create a component to add to the frame; in this case a text area with sample text      Component textArea = new TextArea("Move mouse here to see mouse motion info...");      // Add a mouse motion listener to capture mouse motion events      textArea.addMouseMotionListener(new MouseMotionAdapter() {          public void mouseMoved(MouseEvent evt) {                TextArea source = (TextArea) evt.getSource();              // Process current position of cursor while all mouse buttons are up.                source.setText(source.getText() + "\nMouse moved [" + evt.getPoint().x + "," + evt.getPoint().y + "]");          }          public void mouseDragged(MouseEvent evt) {                TextArea source = (TextArea) evt.getSource();              // Process current position of cursor while mouse button is pressed.                source.setText(source.getText() + "\nMouse dragged [" + evt.getPoint().x + "," + evt.getPoint().y + "]");          }      });      // Add the components to the frame; by default, the frame has a border layout      frame.add(textArea, BorderLayout.NORTH);      // Show the frame      int width = 300;      int height = 300;      frame.setSize(width, height);      frame.setVisible(true);  }}
相关栏目:

用户点评