标签:
1 import java.awt.Graphics; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 public class Test_16_17 extends JFrame{ 6 7 public Test_16_17(){ 8 add(new JP()); 9 } 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 Test_16_17 frame = new Test_16_17(); 13 frame.setSize(300,220); 14 frame.setTitle("Test_16_16"); 15 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 16 frame.setLocationRelativeTo(null); // Center the frame 17 frame.setVisible(true); 18 } 19 20 static class JP extends JPanel{ 21 private int x = 0 , y = 20; 22 private String str; 23 public JP(){ 24 addMouseListener(new MouseAdapter(){ 25 public void mousePressed(MouseEvent e){ 26 str = "good"; 27 } 28 public void mouseReleased(MouseEvent e){ 29 str = ""; 30 } 31 }); 32 Timer timer = new Timer(1000,new TimerListener()); 33 timer.start(); 34 } 35 protected void paintComponent(Graphics g){ 36 37 if(x > getWidth()) x = 0; 38 x += 5; 39 if(str != "good") 40 {super.paintComponent(g); g.drawString("this place",x,y); } 41 } 42 class TimerListener implements ActionListener{ 43 44 @Override 45 public void actionPerformed(ActionEvent arg0) { 46 // TODO Auto-generated method stub 47 repaint(); 48 } 49 50 } 51 } 52 }
标签:
原文地址:http://www.cnblogs.com/wanjiang/p/5657521.html