实现效果:
奔溃的线程侠:(单线程)
主线程正在处理刷新图片的请求时,无法再接受其他请求,从而陷入阻塞的死循环状态。
绘制图片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import javax.swing.JPanel; public class CartonPerson extends JPanel implements Runnable{ Image img[]= new Image[ 6 ]; int index= 0 ; int speed; public CartonPerson( int speed){ this .speed=speed; img[ 0 ]=Toolkit.getDefaultToolkit().getImage( "1.png" ); img[ 1 ]=Toolkit.getDefaultToolkit().getImage( "2.png" ); img[ 2 ]=Toolkit.getDefaultToolkit().getImage( "3.png" ); img[ 3 ]=Toolkit.getDefaultToolkit().getImage( "4.png" ); img[ 4 ]=Toolkit.getDefaultToolkit().getImage( "5.png" ); img[ 5 ]=Toolkit.getDefaultToolkit().getImage( "6.png" ); } public void run(){ while ( true ){ try { repaint(); Thread.sleep( 100 ); } catch (InterruptedException e){ e.printStackTrace(); } } } @Override public void paintComponent(Graphics g) { // TODO Auto-generated method stub super .paintComponent(g); g.drawImage(img[index], 0 , 0 , getWidth(), getHeight(), this ); // System.out.println(index); if (index== 5 ){ index= 0 ; } else { index++; } } } |
单线程的窗体布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class SingleThreadCarton extends JFrame{ CartonPerson p1; JButton bstart= new JButton( "开始" ); JButton bpause= new JButton( "稍等" ); JButton bresume= new JButton( "继续" ); SingleThreadCarton(){ init(); this .setTitle( "奔溃的线程侠" ); this .setSize( 600 , 500 ); this .setResizable( true ); this .setLocationRelativeTo( null ); this .setDefaultCloseOperation(EXIT_ON_CLOSE); this .setVisible( true ); } void init(){ this .setLayout( null ); p1= new CartonPerson( 0 ); p1.setBounds( 260 , 100 , 80 , 160 ); bstart.setBounds( 260 , 280 , 80 , 30 ); bpause.setBounds( 260 , 320 , 80 , 30 ); bresume.setBounds( 260 , 360 , 80 , 30 ); this .add(p1); this .add(bstart); this .add(bpause); this .add(bresume); ButtonClick bc= new ButtonClick(); bstart.addActionListener(bc); bpause.addActionListener(bc); bresume.addActionListener(bc); } class ButtonClick implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource()==bstart){ p1.run(); } else if (e.getSource()==bpause){ } else if (e.getSource()==bresume){ } } } public static void main(String[] args){ new SingleThreadCarton(); } } |
运行结果:
点击“开始”按钮后,程序奔溃。
多线程的窗体布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class MultiThreadCarton extends JFrame{ CartonPerson p1; Thread t1; JButton bstart= new JButton( "开始" ); JButton bpause= new JButton( "稍等" ); JButton bresume= new JButton( "继续" ); MultiThreadCarton(){ init(); this .setTitle( "奔跑的线程侠" ); this .setSize( 600 , 500 ); this .setResizable( true ); this .setLocationRelativeTo( null ); this .setDefaultCloseOperation(EXIT_ON_CLOSE); this .setVisible( true ); } void init(){ this .setLayout( null ); p1= new CartonPerson( 0 ); p1.setBounds( 260 , 100 , 80 , 160 ); bstart.setBounds( 260 , 280 , 80 , 30 ); bpause.setBounds( 260 , 320 , 80 , 30 ); bresume.setBounds( 260 , 360 , 80 , 30 ); this .add(p1); this .add(bstart); this .add(bpause); this .add(bresume); ButtonClick bc= new ButtonClick(); bstart.addActionListener(bc); bpause.addActionListener(bc); bresume.addActionListener(bc); t1= new Thread(p1); } class ButtonClick implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource()==bstart){ // p1.run(); t1.start(); } else if (e.getSource()==bpause){ t1.suspend(); } else if (e.getSource()==bresume){ t1.resume(); } } } public static void main(String[] args){ new MultiThreadCarton(); } } |
运行结果:如顶图所示。
以上就是本次小编给大家带来的关于java中Swing会奔跑的线程侠这个示例的讲述,感谢大家对服务器之家的支持。
本文转载于:https://www.idaobin.com/archives/841.html