版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!
恰饭广告
实现效果:
奔溃的线程侠:(单线程)
主线程正在处理刷新图片的请求时,无法再接受其他请求,从而陷入阻塞的死循环状态。
绘制图片
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++; } } }
单线程的窗体布局
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(); } }
运行结果:
点击“开始”按钮后,程序奔溃。
多线程的窗体布局
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(); } }
运行结果:如顶图所示。
下载链接如下(包含图片)
原文链接:https://www.idaobin.com/archives/841.html
让我恰个饭吧.ヘ( ̄ω ̄ヘ)
恰饭广告