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

📄 习题5-3.java

📁 JAVA用Java和XML建立试题库管理系统,试题库管理系统的设计
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class tt1
{
  public static void main(String args[])
  {
    JFrame f=new TimerTestFrame();
    f.show();
  }
}

class TimerTestFrame extends JFrame
{
  public TimerTestFrame()
  {
     setSize(300,150);
     setTitle("TimerTest");
     addWindowListener(new WindowAdapter()
     {
        public void windowClosing(WindowEvent e)
        {
          System.exit(0);
        }
     });
     
     Container c=getContentPane();
     c.setLayout(new GridLayout(1,2));
     ClockCanvas pp=new ClockCanvas();
     Button bb=new Button("Click Me");
     bb.addActionListener(pp);
     c.add(bb);
     c.add(pp);
  }
}

interface TimerListener
{
  void timeElapsed(Timer t);
}

class Timer extends Thread
{
  public Timer(int i,TimerListener t)
  {
    target=t;interval=i;
  }
  public void run()
  {
    try 
    {
        sleep(interval);
    }
    catch(InterruptedException e){}
    target.timeElapsed(this);
  }
  
  private TimerListener target;
  private int interval;
}

class ClockCanvas extends JPanel implements TimerListener,ActionListener
{
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    if(show)
    {
    	g.setColor(color);
    	g.fillRect(x,y,w,h);
    }
  }
  public void timeElapsed(Timer t)
  {
	show=false;
    repaint();
  }
  public void actionPerformed(ActionEvent e)
  {
    int width=getSize().width;
    int height=getSize().height;
    w=(int)(Math.random()*width/3+1);
    h=(int)(Math.random()*height/3+1);
    x=(int)(Math.random()*(width-w));
    x=(int)(Math.random()*(height-h));
    color=new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
    show=true;repaint();
    Timer t=new Timer(3000,this);
    t.start();
    setSize(125,125);  	
  }
  
  boolean show=false;
  private int x=30,y=50,w=10,h=10;
  private Color color=Color.red;
}

⌨️ 快捷键说明

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