欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java中Swing會(huì)奔跑的線程俠

 更新時(shí)間:2018年03月01日 08:06:01   作者:彬菌  
本文通過(guò)代碼示例給大家詳細(xì)講解了java中Swing會(huì)奔跑的線程俠這個(gè)經(jīng)典的示例,有興趣的朋友學(xué)習(xí)下。

實(shí)現(xiàn)效果:

奔潰的線程俠:(單線程)

主線程正在處理刷新圖片的請(qǐng)求時(shí),無(wú)法再接受其他請(qǐng)求,從而陷入阻塞的死循環(huán)狀態(tài)。

繪制圖片

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("開(kāi)始");
	JButton bpause=new JButton("稍等");
	JButton bresume=new JButton("繼續(xù)");
	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();
	}
}

運(yùn)行結(jié)果:

點(diǎn)擊“開(kāi)始”按鈕后,程序奔潰。

多線程的窗體布局

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("開(kāi)始");
	JButton bpause=new JButton("稍等");
	JButton bresume=new JButton("繼續(xù)");
	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();
	}
}

運(yùn)行結(jié)果:如頂圖所示。

以上就是本次小編給大家?guī)?lái)的關(guān)于java中Swing會(huì)奔跑的線程俠這個(gè)示例的講述,感謝大家對(duì)腳本之家的支持。

本文轉(zhuǎn)載于:https://www.idaobin.com/archives/841.html

相關(guān)文章

最新評(píng)論