e7_2.java

来自「java 初学者学习实例」· Java 代码 · 共 47 行

JAVA
47
字号
import java.applet.*;import java.awt.*;import java.awt.event.*;
  public class E7_2 extends Applet implements MouseListener
  {  TextField text;int x,y, tom;
      public void init ()
      {  x=5; tom=0; text=new TextField(40);
         add(text) ;addMouseListener (this)  ;
    }
   public void paint (Graphics g)
      {if (tom==1)
         {g. setColor (Color. red);
           g.fillOval(10+x,10+y,15,15);
          g.setColor(Color.green); g.fillOval(100,100,30,30);
          g.setColor(Color.red); g.fillOval(120,120,35,35);
       }
      else if(tom==-1)
         {g.setColor(Color.pink); g.fillOval(100,100,50,50);
          g.setColor(Color.white); g.fillOval(120,120,45,45);
      }
      else if (tom==2)
        { g. setColor (Color. red);
       g. drawString (   "来了,作业带来了吗? ",80,120);
      }
      else if (tom==-2)
        { g.setColor (Color. green);
         g.drawString( "欢迎再来,别忘了关门", 80,120) ;
      }
}
public void mousePressed(MouseEvent e)
   { tom=1; x=e.getX(); y=e.getY();
       text.setText ("鼠标按下了,位置是: "+e.getX()+","+e.getY() );
     repaint ( );}
  public void mouseReleased(MouseEvent e)
    {  tom=-1;text.setText(" 鼠标松开了,位置是:"+e.getX ( ) +", "+e.getY ( ) );
      repaint ( );}
    public void mouseEntered(MouseEvent e)
{tom=2; text.setText( "~鼠标进来了,位置是:"+e.getX ( ) +", "+e.getY( ) );
  repaint();
}
public void mouseExited(MouseEvent e)
{tom=-2; text.setText("鼠标走开了");
  repaint();
}
public void mouseClicked(MouseEvent e)
{ if(e.getClickCount()==2)
      { text.setText("双击鼠标,位置:"+e.getX()+","+e.getY( ));
     }
}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?