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

16.20

时间:2016-07-10 18:14:37      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

技术分享
 1 import java.awt.Graphics;
 2 import java.awt.event.*;
 3 import javax.swing.*;
 4 
 5 public class Test_16_20 extends JFrame{
 6     
 7     public Test_16_20(){
 8         add(new JP());
 9     }
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         Test_16_20 frame = new Test_16_20();        
13         frame.setSize(300,300);
14         frame.setTitle("Test_16_20");
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_getX,y_getY,xCenter,yCenter;
22         private String str="";
23         public JP(){
24             addMouseMotionListener(new MouseMotionAdapter(){
25                 public void mouseMoved(MouseEvent e){
26                     x_getX = e.getX();
27                     y_getY = e.getY();
28                     if(x_getX > xCenter - 50 && x_getX < xCenter + 50
29                     && y_getY >yCenter-20 && y_getY < yCenter+20)
30                     str = "in the rect";
31                     else str = "out of the rect";
32                     repaint();
33                 } 
34             });
35         }
36         protected void paintComponent(Graphics g){
37             xCenter = getWidth()/2;
38             yCenter = getHeight()/2;
39             
40             super.paintComponent(g);
41             g.drawRect(xCenter - 50, yCenter - 20, 100, 40);
42             g.drawString(str, x_getX, y_getY);
43         }
44     }
45 }
Test_16_20.java

效果图:

技术分享

 

16.20

标签:

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

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