• 16.20


     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

    效果图:

  • 相关阅读:
    20181126-java-面试知识-收集
    redis学习
    ThoughtWorks.QRCode类库
    Microsoft Enterprise Library
    DocX插件
    Aspose 插件
    工厂示例
    面向对象
    WebSocket 是什么原理?为什么可以实现持久连接?
    label标签的作用
  • 原文地址:https://www.cnblogs.com/wanjiang/p/5658231.html
Copyright © 2020-2023  润新知