package 18.conp;
import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.util.Random;
import javax.swing.JLabel;
public class MyRain extends JLabel implements Runnable {
private boolean isRun=true;
private String typeMsg="";
public String getTypeMsg() {
return typeMsg;
}
public void setTypeMsg(String typeMsg) {
int i = typeMsg.length();
if(i<=0)
return;
String s = Text().substring(0, i);
if(s.equals(typeMsg))
{
if(i==Text().length())
{
this.setVisible(false);
isRun = false;
}
this.setForeground(Color.RED);
}
else
{
this.setForeground(Color.BLACK);
}
}
public boolean isRun() {
return isRun;
}
public void setRun(boolean isRun) {
this.isRun = isRun;
}
public MyRain(String msg)
{
Random rnd = new Random();
int i = Int(780);
this.setText(msg);
this.setBounds(i, 0, msg.length()*10, 40);
Thread th = new Thread(this);
th.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(isRun)
{
Point p = Location();
p.y = p.y+2;
this.setLocation(p);
if(p.y>560)
{
isRun = false;
System.out.println("完成");
}
try {
Thread.currentThread().sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package 18.form;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import 18.util.RainBuilder;
public class FormMain extends JFrame implements KeyListener{
JPanel pane = new JPanel();
RainBuilder rb = new RainBuilder(pane);
public FormMain()
{
rb.start();
this.add(pane);
this.setSize(800,600);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addKeyListener(this);
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
rb.KeyChar());
}
}
package 18.util;
import java.util.ArrayList;
import java.util.Iterator;java valueof
import java.util.List;
import java.util.Random;
import javax.swing.JPanel;
import 18.conp.MyRain;
public class RainBuilder extends Thread{
private boolean isRun = true;
private int width=2;
private JPanel pane;
private byte maxNum = 5;
private List<MyRain> rains=new ArrayList<MyRain>();
private MyRain nowTypeRain=null;  //abc
//private final String SHOW="";
public RainBuilder(JPanel pane)
{
this.pane = pane;
this.pane.setLayout(null);
}
@Ov
erride
public void run() {
while(isRun)
{
MyRain rain = new MyRain(getMsg());
rains.add(rain);
pane.add(rain);
try {
this.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println("T");
removeDieRain();
}
}
private void removeDieRain()
{
for(int i=rains.size()-1;i>=0;i--)
{
MyRain m = ((i);
if(!m.isRun())
{
System.out.println("移除完成的雨滴!");
}
}
}
private String getMsg()
{
Random rnd = new Random();
int k = Int(maxNum-1);
String s="";
for(int i=0;i<k;i++)
{
char ch = (char)('a'+Int('z'-'a')-1);
s = s+String.valueOf(ch);
}
System.out.println(s);
return String.valueOf(s);
}
public void keyPressEvents(char ch)
{
if(nowTypeRain != null && nowTypeRain.isRun())
{
nowTypeRain.TypeMsg()+ch);
TypeMsg().equals(""))
nowTypeRain = null;
else
return;
}
Iterator it = rains.iterator();
while(it.hasNext())
{
MyRain m = (();
m.TypeMsg()+ch);
if(!m.getTypeMsg().equals(""))
{
nowTypeRain = m;
return;
}
}
}
}
package 18;
import 18.form.FormMain;
public class No18Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FormMain frm = new FormMain();
}
}