⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ballframe.java

📁 有关定时器的使用 你可从中体会到其方法 Timer类的使用
💻 JAVA
字号:
import   java.awt.*;   
  import   javax.swing.*;   
  import   java.awt.event.*;   
    
  public class   ballFrame   extends   JFrame   implements   ActionListener   
  {   
  private  Timer   timers;   
  private   int   dx,dy,x,y;   
  private   int   add;   
  private   int   startx,starty;   
  private   JButton   jb;   
  private   Color   c;   
  private   int   xchose,ychose;   
  private   int   upline;   
  private   boolean   flag;   
  public   ballFrame()   
  {   
  Container   ctp=getContentPane();   
  ctp.setLayout(null);   
  dx=10;   
  dy=20;   
  startx=60;   
  starty=50;   
  x=5;   
  y=7;   
  flag=true;   
  setBackground(Color.black);   
  c=getBackground();   
  jb=new   JButton   ("Start");   
  ctp.add(jb);   
  jb.setBounds(20,350,70,20);   
  jb.addActionListener(this);   
  timers=new   Timer(80,this);   
  //g.dispose();   
  addWindowListener(new   WindowAdapter()   
  {   
  public   void   windowClosing(WindowEvent   e)   
  {   
  System.exit(0);   
  }   
  });   
  setSize(700,400);   
  setResizable(false);   
  setTitle("ball");   
  setVisible(true);   
  }   
  public   void   paint(Graphics   g)   
  { //super.paint(g);   
  g.setColor(Color.red);   
  g.fillOval(dx,dy,10,10);   
  //this.setBackground(Color.black);   
  g.setColor(Color.green);   
  for   (int   i=0   ;i<=9;i++)   
  {   
  g.drawLine((startx*(i+1)),(starty+(i*30)),(startx*(i+1)),(starty+(i+1)*30));   
  g.drawLine((startx*i),(starty+(i*30)),(startx*(i+1)),(starty+i*30));   
  }   
  g.drawLine(600,350,700,350);   
  //g.dispose();   
  }   
  public   void   move()   
  {   
    
  //add=5;   
  Graphics   g=getGraphics();   
  g.setColor(c);   
  g.fillOval(dx,dy,10,10);   
  g.setColor(Color.red);   
  dx=dx+x;   
  dy=dy+y;   
  g.fillOval(dx,dy,10,10);   
  g.dispose();   
  for   (int   i=0   ;i<=9;i++)   
  if   (   dx>=i*60   &   (dx<60*(i+1)))   xchose=i+1;   
  for   (int   i=0;i<=9;i++)   
  if   (dy>(((i*30)+50)-15))             ychose=i+1;   
  /*for   (int   i=0;   i<=9;i++)   
  if   (dy<((i*10)+30))   upline=1;   
        else         upline=0;*/   
  if   (dy<30)   upline=1;   
  else   upline=0;         
          
  switch   (xchose)   
  {   
  case   1:if   (ychose==1)   y=-5;   
  else     
                  if(   upline==1)     
  y=5;   
  break;   
  case   2:if   (ychose==2)   y=-5;   
  else     
  if   (upline==1)   
  y=5;   
  break;     
  case   3:   if   (ychose==3)   y=-5;   
  else   
  if   (upline==1)   
  y=5;   
  break;   
  case   4:   if   (ychose==4)   y=-5;   
  else   
  if   (upline==1)   
  y=5;   
  break   ;   
  case   5:if   (ychose==5)   y=-5;   
  else   
  if   (upline==1)   
  y=5;   
  break   ;   
  case   6:if   (ychose==6)   y=-5;   
  else   
  if   (upline==1)   
  y=5;   
  break   ;   
  case   7:if   (ychose==7)   y=-5;   
  else   
  if   (upline==1)   
  y=5;   
  break   ;   
  case   8:if   (ychose==8)   y=-5;   
  else   
  if   (upline==1)   
  y=5;   
  break   ;   
  case   9:if   (ychose==9)   y=-5;   
  else   
  if   (upline==1)   
  y=5;   
  break   ;   
  case   10:if   (ychose==10)   y=-5;   
  else   
  if   (upline==1)   
  y=5;   
  break   ;   
  default:break   ;   
  }   
    
  }   
  public   void   actionPerformed(ActionEvent   evt   )   
  {   
  Object   source=evt.getSource();   
  if   (source==jb)     
  {     
  if   (jb.getText()=="Start")   
  {   
  jb.setText("Stop");   
  timers.start();   
  }   
  else   
        {     
        jb.setText("Start");   
        timers.stop();   
        }   
  }else   
  move();   
  }   
  }   
  class   ball   
  {   
  public     static   void   main   (String   args[])   
  {   
  JFrame   test=new   ballFrame();   
  test.setVisible(true);   
  //test.repaint();   
  }   
  }   

⌨️ 快捷键说明

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