1 /* 2 鼠标单击面板,在单击处画一个圆 3 */ 4 import java.awt.*; 5 import java.awt.event.*; 6 import javax.swing.*; 7 8 9 public class MouseMessage extends JFrame 10 { 11 public MouseMessage() 12 { 13 MousePanel p = new MousePanel(); 14 add(p); 15 } 16 17 public static void main(String[] args) 18 { 19 MouseMessage frame = new MouseMessage(); 20 frame.setTitle("show"); 21 frame.setSize(240,480); 22 frame.setLocationRelativeTo(null); 23 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 24 frame.setVisible(true); 25 } 26 27 } 28 29 //面板类 30 class MousePanel extends JPanel 31 { 32 private int x = 0,y = 0; 33 public MousePanel() 34 { 35 addMouseMotionListener(new MouseMotionAdapter() 36 { 37 public void mouseClicked(MouseEvent e) 38 { 39 x = e.getX(); 40 y = e.getY(); 41 //repaint(); 42 } 43 }); 44 } 45 protected void paintComponent(Graphics g) 46 { 47 super.paintComponent(g); 48 g.setColor(Color.RED); 49 g.fillOval(x,y,10,10); 50 } 51 }