栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java中Swing会奔跑的线程侠

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java中Swing会奔跑的线程侠

实现效果:

奔溃的线程侠:(单线程)

主线程正在处理刷新图片的请求时,无法再接受其他请求,从而陷入阻塞的死循环状态。

绘制图片

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();
	}
}

运行结果:如顶图所示。

以上就是本次小编给大家带来的关于java中Swing会奔跑的线程侠这个示例的讲述,感谢大家对考高分网的支持。

本文转载于:https://www.idaobin.com/archives/841.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/141528.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号