鼠标键盘处理
作者:互联网
1 package ch11_5; 2 3 import java.awt.FlowLayout; 4 import java.awt.HeadlessException; 5 import java.awt.event.*; 6 7 import javax.swing.*; 8 9 public class MouseEventTest extends JFrame implements MouseMotionListener,MouseListener{ 10 private JLabel jl1,jl2; 11 private JTextArea jta; 12 private JTextField jtf1,jtf2; 13 14 public MouseEventTest() { 15 super("事件使用实例_鼠标事件"); 16 jl1=new JLabel("X: "); 17 jl2=new JLabel("Y: "); 18 jtf1=new JTextField(5); 19 jtf2=new JTextField(5); 20 jta=new JTextArea(15,15); 21 this.setBounds(500, 500, 500, 500); 22 this.setLayout(new FlowLayout(FlowLayout.LEFT)); 23 this.add(jl1); 24 this.add(jl2); 25 this.add(jtf1); 26 this.add(jtf2); 27 this.add(jta); 28 this.addMouseMotionListener(this); 29 this.addMouseListener(this); 30 } 31 32 @Override 33 public void mouseClicked(MouseEvent e) { 34 jta.setText(jta.getText()+"你单击鼠标了!\n"); 35 36 } 37 38 @Override 39 public void mousePressed(MouseEvent e) { 40 jta.setText(jta.getText()+"你按下鼠标了!\n"); 41 42 } 43 44 @Override 45 public void mouseReleased(MouseEvent e) { 46 jta.setText(jta.getText()+"你松开鼠标了!\n"); 47 48 } 49 50 @Override 51 public void mouseEntered(MouseEvent e) { 52 jta.setText(jta.getText()+"你使用鼠标进入组件区域了!\n"); 53 54 } 55 56 @Override 57 public void mouseExited(MouseEvent e) { 58 jta.setText(jta.getText()+"你使用鼠标离开组件区域了!\n"); 59 60 } 61 @Override 62 public void mouseDragged(MouseEvent e) { 63 jta.setText(jta.getText()+"你拖动鼠标了!\n"); 64 65 } 66 67 @Override 68 public void mouseMoved(MouseEvent e) { 69 int x,y; 70 x=e.getX(); 71 y=e.getY(); 72 jtf1.setText(""+x); 73 jtf2.setText(""+y); 74 } 75 public static void main(String[] args) { 76 MouseEventTest a=new MouseEventTest(); 77 a.setVisible(true); 78 a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 79 80 } 81 82 83 84 }
标签:鼠标,处理,void,键盘,setText,new,jta,public 来源: https://www.cnblogs.com/dss-99/p/14174753.html