java中Swing会奔跑的线程侠
时间:2021-04-16 08:25:55|栏目:JAVA代码|点击: 次
实现效果:

奔溃的线程侠:(单线程)
主线程正在处理刷新图片的请求时,无法再接受其他请求,从而陷入阻塞的死循环状态。
绘制图片
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();
}
}
运行结果:如顶图所示。
上一篇:SpringMvc后台接收json数据中文乱码问题详解
栏 目:JAVA代码
下一篇:springboot中事务管理@Transactional的注意事项与使用场景
本文标题:java中Swing会奔跑的线程侠
本文地址:http://www.codeinn.net/misctech/102503.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




