java中Swing会奔跑的线程侠
作者:彬菌 时间:2021-12-14 23:47:36
实现效果:
奔溃的线程侠:(单线程)
主线程正在处理刷新图片的请求时,无法再接受其他请求,从而陷入阻塞的死循环状态。
绘制图片
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
标签:java,Swing
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 手写LRU缓存淘汰算法
2023-03-03 00:11:19
![](https://img.aspxhome.com/file/2023/0/64100_0s.jpg)
使用eclipse创建java项目的方法
2022-10-15 04:41:00
![](https://img.aspxhome.com/file/2023/0/64010_0s.jpg)
Java实现顺序表和链表结构
2023-11-13 09:35:43
![](https://img.aspxhome.com/file/2023/2/62832_0s.jpg)
java中List删除时需要的注意事项
2023-11-11 01:28:00
如何利用Spring把元素解析成BeanDefinition对象
2023-11-23 05:17:16
![](https://img.aspxhome.com/file/2023/9/59939_0s.png)
Springboot全局异常捕获及try catch区别解析
2022-03-02 17:44:07
![](https://img.aspxhome.com/file/2023/7/59227_0s.png)
深入解析java中的locale
2023-11-09 18:14:20
Java单例模式分析
2023-11-16 03:17:06
Flutter基于Dart Unwrapping Multiple Optional小技巧
2023-07-05 11:44:19
Spark JDBC操作MySQL方式详细讲解
2021-05-24 12:41:51
Java KindEditor粘贴图片自动上传到服务器功能实现
2023-08-07 01:42:33
![](https://img.aspxhome.com/file/2023/2/57902_0s.gif)
Java创建类模式_动力节点Java学院整理
2023-04-15 23:11:55
解决Jenkins集成SonarQube遇到的报错问题
2023-11-24 08:54:10
![](https://img.aspxhome.com/file/2023/6/64396_0s.png)
SpringBoot实现接口数据的加解密功能
2023-06-30 00:11:01
![](https://img.aspxhome.com/file/2023/8/63008_0s.png)
Spring boot jpa 删除数据和事务管理的问题实例详解
2022-07-02 12:11:43
spring boot如何实现切割分片上传
2023-06-29 02:49:26
![](https://img.aspxhome.com/file/2023/7/57487_0s.png)
Java try-with-resource语法使用解析
2021-10-09 02:28:37
Java超详细教你写一个银行存款系统案例
2022-01-04 22:33:59
![](https://img.aspxhome.com/file/2023/4/62434_0s.png)
实例解析JAVA中代码的加载顺序
2021-10-26 14:57:22
![](https://img.aspxhome.com/file/2023/8/61038_0s.png)
总结一次C++ 程序优化历程
2023-11-02 22:38:30