*一個典型的多線程小實例
*幾乎每一處都有注釋
*適合初學者學習交流
*/
import java
import java
import javax
import javax
public class ball
{
public static void main(String args[])
{
new frame();
}
}
class frame extends JFrame implements ActionListener
{
JPanel jp_HuaBan;
JPanel jp_YuLan;
JSlider js_red;
JSlider js_green;
JSlider js_blue;
JSlider js_size;
int x=
int ball_size=
public frame()
{
super(
//創建畫板
jp_HuaBan=new JPanel();
jp_HuaBan
jp_HuaBan
//創建球的予覽框
jp_YuLan=new JPanel();
jp_YuLan
jp_YuLan
//創建一個文本框作為狀態欄
JTextField jt_status=new JTextField(
jt_status
jt_status
//創建紅
JLabel jl_red=new JLabel(
jl_red
JLabel jl_green=new JLabel(
jl_green
JLabel jl_blue=new JLabel(
jl_blue
JLabel jl_size=new JLabel(
jl_size
//創建紅
js_red=new JSlider(SwingConstants
js_red
//填充滑塊
js_red
//繪制勾號標記
js_red
//主要的勾號標記大小
js_red
//次要的勾號標記大小
js_red
//顯示主要刻度的數字標記
js_red
//添加監聽者
js_red
js_green=new JSlider(SwingConstants
js_green
js_green
js_green
js_green
js_green
js_green
js_green
js_blue=new JSlider(SwingConstants
js_blue
js_blue
js_blue
js_blue
js_blue
js_blue
js_blue
//創建球的大小的滑塊
js_size=new JSlider(SwingConstants
js_size
js_size
js_size
js_size
js_size
js_size
js_size
//創建按鈕
JButton jb=new JButton(
jb
jb
//將所有對象添加到窗體
Container c=this
c
c
c
c
c
c
c
c
c
c
c
c
c
//設置窗體的位置和大小
this
this
this
this
{
public void windowClosing(WindowEvent e)
{
System
}
});
}
public void actionPerformed(ActionEvent e)
{
Color ball_color=new Color(js_red
js_green
rball r=new rball(jp_HuaBan
r
lball l=new lball(jp_HuaBan
l
}
public void stateChanged(ChangeEvent e)
{
//獲得jp_YuLan的畫筆並在jp_YuLan的中心畫個圓
Graphics g=jp_YuLan
g
g
x=
y=
ball_size=js_size
g
js_green
g
g
}
}
class lball extends Thread
{
//從窗口左面出現的球
JPanel jp_HuaBan;
int ball_size;
Color ball_color;
public lball(JPanel j
{
//獲得畫板的句柄
jp_HuaBan=j;
//獲得球的大小
ball_size=i;
//獲得球的顏色
ball_color=c;
}
public void run()
{
Graphics g=jp_HuaBan
int x=
//計算畫球時X軸
int HuaBan_x=
int HuaBan_y=
//球移動的增量
int x_increase=
while(true)
{
//將上一次畫的球擦掉
g
g
//畫球
g
x=x+x_increase;
y=y+y_increase;
g
//判斷球是否到達了邊界
if(x<=
if(y<=
try
{
//休眠一段時間
Thread
}
catch(Exception e){}
}
}
}
class rball extends Thread
{
//從窗口左面出現的球
JPanel jp_HuaBan;
int ball_size;
Color ball_color;
public rball(JPanel j
{
jp_HuaBan=j;
ball_size=i;
ball_color=c;
}
public void run()
{
Graphics g=jp_HuaBan
int x=
int HuaBan_x=x;
int HuaBan_y=
int x_increase=
while(true)
{
g
g
g
x=x+x_increase;
y=y+y_increase;
g
if(x<=
if(y<=
try
{
Thread
}
catch(Exception e){}
}
}
}
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27730.html