码迷,mamicode.com
首页 > 其他好文 > 详细

16.17

时间:2016-07-10 14:06:25      阅读:544      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

技术分享
 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 }
Test_16_17.java

 

16.17

标签:

原文地址:http://www.cnblogs.com/wanjiang/p/5657521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!