資源簡介
編寫一個Applet內部顯示一個藍色反彈球的程序,通過一個事件使該球開始運動,在該球撞擊Applet邊框時,它應從邊框彈回并以相反的方向運動。
代碼片段和文件信息
/**
?*?@(#)實驗六2_線程.java
?*
?*?Sample?applet?application
?*
?*?@author?
?*?@version?1.00?09/11/20
?*/
?
import?java.awt.*;
import?java.applet.*;
import?java.awt.event.*;
public?class?Thread2?extends?applet?implements?RunnableActionListener
{
Label?prompt;
TextField?text;
Color?blueColor=new?Color(00255);
Button?btn1btn2;
Thread?thread1;
int?X=0;int?Y=0;
public?void?init()?
{
prompt=new?Label(“速度選擇“);
text=new?TextField(10);
btn1=new?Button(“開始“);
add(prompt);
add(text);
add(btn1);
btn1.addActionListener(this);
btn2=new?Button(“停止“);
add(btn2);
btn2.addActionListener(this);
}
public?void?paint(Graphics?g)?
{
g.setColor(blueColor);
g.fillOval(XY5050);
}
public?void?actionPerformed(ActionEvent?e)
{
if(e.getSource()==btn1)
{
???thread1=new?Thread(this);
???thread1.start();
}
if(e.getSource()==btn2)
{
if(thread1.isAlive())
??????thread1.stop();
}
}
評論
共有 條評論