Java模拟音乐播放器 暂停与重新播放——线程如何控制另外一个线程的状态
package com.example.Thread; import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map; /**
* Created by 达达队长 on 2018/4/28.
*/ class Count extends Thread { private volatile int stop = 1;
public volatile int i = 0;
private JLabel count; public Count(JLabel count) {
this.count = count;
} @Override
public void run() {
try {
synchronized (this) {
while (true) {
if (stop == 0) {
wait();
} else if (stop == 1) {
count.setText(i++ + "");
} else if (stop == 2) {
return;
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} public void setStop() {
stop = 0;
} public synchronized void setStart() {
stop = 1;
notify();
} public void setKill() {
stop = 2;
}
} public class MyTimer { public static int lableSize = 1;
public static Map<JButton, Count> threadMap = new HashMap<JButton, Count>(16); public static void main(String[] args) { // 创建 JFrame 实例
JFrame frame = new JFrame("多线条启停");
// Setting the width and height of frame
frame.setSize(1080, 720);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /* 创建面板,这个类似于 HTML 的 div 标签
* 我们可以创建多个面板并在 JFrame 中指定位置
* 面板中我们可以添加文本字段,按钮及其他组件。
*/ /*
* 调用用户定义的方法并添加组件到面板
*/
placeComponents(frame); // 设置界面可见
frame.setVisible(true);
} private static void placeComponents(final JFrame frame) { final JPanel panel = new JPanel(); /* 布局部分我们这边不多做介绍
* 这边设置布局为 null
*/
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.add(panel); // 创建登录按钮
JButton loginButton = new JButton("添加计数器");
loginButton.setBounds(10, 10, 80, 25);
panel.add(loginButton);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 创建 JLabel
JLabel userLabel = new JLabel("计数器" + lableSize + ":");
userLabel.setBounds(10, 30 * lableSize, 40, 25);
panel.add(userLabel); final JLabel count = new JLabel("0");
count.setBounds(100, 30 * lableSize, 40, 25);
panel.add(count); final JButton button = new JButton("启动");
button.setBounds(200, 30 * lableSize, 80, 25);
panel.add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { if (button.getText().equals("启动")) {
Count thread = threadMap.get(button);
if (null == thread) {
thread = new Count(count);
threadMap.put(button, thread);
thread.start();
} else {
thread.setStart();
}
button.setText("暂停"); } else if (button.getText().equals("暂停")) {
Count thread = threadMap.get(button);
thread.setStop();
button.setText("启动"); }
}
}); final JButton kill = new JButton("停止");
kill.setBounds(300, 30 * lableSize, 80, 25);
panel.add(kill);
kill.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Count thread = threadMap.get(button);
if (null == thread) { } else {
thread.setKill();
System.out.println(thread.isAlive());
count.setText("0");
threadMap.remove(button);
button.setText("启动");
}
}
}); lableSize++;
frame.repaint();
}
});
} }
Java模拟音乐播放器 暂停与重新播放——线程如何控制另外一个线程的状态的更多相关文章
- html 音乐 QQ播放器 外链 代码 播放器 外链 代码
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha QQ播放器 外链 代码 播放器 外链 代码 ======== 歌曲链接 QQ播放器 外链 ...
- 搭建rtmp直播流服务之4:videojs和ckPlayer开源播放器二次开发(播放rtmp、hls直播流及普通视频)
前面几章讲解了使用 nginx-rtmp搭建直播流媒体服务器; ffmpeg推流到nginx-rtmp服务器; java通过命令行调用ffmpeg实现推流服务; 从数据源获取,到使用ffmpeg推流, ...
- FFmpeg简易播放器的实现-音频播放
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10068490.html 基于FFmpeg和SDL实现的简易视频播放器,主要分为读取视频文 ...
- Android 仿百度网页音乐播放器圆形图片转圈播放效果
百度网页音乐播放器的效果 如下 : http://www.baidu.com/baidu?word=%E4%B8%80%E7%9B%B4%E5%BE%88%E5%AE%89%E9%9D%99& ...
- Android播放器推荐:可以播放本地音乐、视频、在线播放音乐、视频、网络收音机等
下载链接:http://www.eoeandroid.com/forum.php?mod=attachment&aid=MTAxNTczfGMyNjNkMzFlfDEzNzY1MzkwNTR8 ...
- android实现gif图播放、暂停、继续播放
之前做过一个项目,在android上实现gif图的播放以及点击屏幕弹出窗口显示gif图片的暂停,之前一直用gifView的jar包实现gif图片的显示,但是在gif暂停.继续播放这块没有找到好的解决方 ...
- EasyPlayer RTSP Windows(with ActiveX/OCX插件)播放器支持H.265播放与抓图功能
EasyPlayer作为业界一款比较优秀的RTSP播放器,一直深受用户的好评,经过了近3年的开发和迭代,从一开始的简单PC版本的RTSP播放功能,到如今支持PC(支持ocx插件).Android.iO ...
- flash播放器插件与flash播放器的区别
flash插件是一个网页ActiveX控件,而flash播放器是一个exe的可执行程序.前者用于播放网页中的falsh动画,而后者用于播放本地swf格式文件.
- java模拟DVD管理器
import java.util.*;import java.text.*;class DVDSet{ String[] name = new String[50]; //名字 int[] ...
随机推荐
- C#数字图像处理算法学习笔记(一)--C#图像处理的3中方法
C#数字图像处理算法学习笔记(一)--C#图像处理的3中方法 Bitmap类:此类封装了GDI+中的一个位图,次位图有图形图像及其属性的像素数据组成.因此此类是用于处理像素数据定义的图形的对象.该类的 ...
- linux 解决 gvfsd-smb-browse CPU 100%占用
原文地址:https://bugzilla.redhat.com/show_bug.cgi?id=1303300 1: sudo vim /etc/samba/smb.conf 2:定位到[globa ...
- HTTP缓存技术,304和200有何区别
为什么有的缓存是 200 OK (from cache),有的缓存是 304 Not Modified 呢?很简单,看运维是否移除了 Entity Tag.移除了,就总是 200 OK (from c ...
- linux命令模式下如何切换首行和尾行
G是到最后一行,gg是到第一行
- jQuery_3_过滤选择器
过滤选择器(过滤器)类似于CSS3里的伪类,包含 1. 基本过滤器 2. 内容过滤器 3. 可见性过滤器 4. 子元素过滤器 5. 其他方法 一. 基本过滤器 过滤器名 jQuery语法 注释 :f ...
- DBMS事务的四大特性
数据库事务的四大特性分别是:原子性.一致性.隔离性和持久性. 特性 说明 实现 一致性 在一个事务执行之前和执行之后数据库都必须处于一致性状态.假如数据库的状态满足所有的完整性约束,也可以说数据库是一 ...
- linux 命令——55 traceroute(转)
通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一 ...
- linux 命令——37 date (转)
在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...
- 日常-acm-排列
用1-9组成三个数abc,def,ghi,每个数字恰好出现一次,要求abc:def:ghi=1:2:3.按照“abc def ghi”输出所有解,每行一个解. #include <iostrea ...
- IOS 移除栈顶的控制器
- (IBAction)back2Two:(id)sender { // 移除当前栈顶的控制器 [self.navigationController popViewControllerAnimated ...