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模拟音乐播放器 暂停与重新播放——线程如何控制另外一个线程的状态的更多相关文章

  1. html 音乐 QQ播放器 外链 代码 播放器 外链 代码

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha QQ播放器 外链 代码 播放器 外链 代码 ======== 歌曲链接 QQ播放器 外链 ...

  2. 搭建rtmp直播流服务之4:videojs和ckPlayer开源播放器二次开发(播放rtmp、hls直播流及普通视频)

    前面几章讲解了使用 nginx-rtmp搭建直播流媒体服务器; ffmpeg推流到nginx-rtmp服务器; java通过命令行调用ffmpeg实现推流服务; 从数据源获取,到使用ffmpeg推流, ...

  3. FFmpeg简易播放器的实现-音频播放

    本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10068490.html 基于FFmpeg和SDL实现的简易视频播放器,主要分为读取视频文 ...

  4. Android 仿百度网页音乐播放器圆形图片转圈播放效果

    百度网页音乐播放器的效果  如下 : http://www.baidu.com/baidu?word=%E4%B8%80%E7%9B%B4%E5%BE%88%E5%AE%89%E9%9D%99& ...

  5. Android播放器推荐:可以播放本地音乐、视频、在线播放音乐、视频、网络收音机等

    下载链接:http://www.eoeandroid.com/forum.php?mod=attachment&aid=MTAxNTczfGMyNjNkMzFlfDEzNzY1MzkwNTR8 ...

  6. android实现gif图播放、暂停、继续播放

    之前做过一个项目,在android上实现gif图的播放以及点击屏幕弹出窗口显示gif图片的暂停,之前一直用gifView的jar包实现gif图片的显示,但是在gif暂停.继续播放这块没有找到好的解决方 ...

  7. EasyPlayer RTSP Windows(with ActiveX/OCX插件)播放器支持H.265播放与抓图功能

    EasyPlayer作为业界一款比较优秀的RTSP播放器,一直深受用户的好评,经过了近3年的开发和迭代,从一开始的简单PC版本的RTSP播放功能,到如今支持PC(支持ocx插件).Android.iO ...

  8. flash播放器插件与flash播放器的区别

    flash插件是一个网页ActiveX控件,而flash播放器是一个exe的可执行程序.前者用于播放网页中的falsh动画,而后者用于播放本地swf格式文件.

  9. java模拟DVD管理器

    import java.util.*;import java.text.*;class DVDSet{    String[] name = new String[50]; //名字    int[] ...

随机推荐

  1. redhat配置dns服务器bind

    配置Oracle11g的RAC需要使用DNS服务器来解析SCAN IP,本文就是以此为例介绍bind服务器的使用.首先科普一下bind服务器,属于企业级产品了,还是开源的: Bind是Berkeley ...

  2. css盒子模型 css3盒子相关样式

    1.内边距(内边距在content外,边框内) 内边距属性: padding          设置所有边距 padding-bottom     底边距 padding-left           ...

  3. 添加egit插件

    1.下载egit插件 打开Eclipse,git需要eclipse授权,通过网页是无法下载egit的安装包的.在菜单栏依次打开eclipse→help→install new software→add ...

  4. JDBC连接数据库(Servlet+JSP)

    JDBC(Java Database connectivity),是连接数据库的一种方式.后面的框架Mybatis和Hibernate等都封装的是JDBC.在JDBC中常用的API有4个:Driver ...

  5. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:7.项目介绍之架构(1)

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 前言 <迷你微信>服务器端是使用Java语言,Mina框架编写的,一个良好的架构关系到后期迭代的方便程度 ...

  6. 【ros】【bug】gtk2\3 冲突

    ORBSLAM2首次运行出现GTK冲突. Gtk-ERROR **: GTK+ 3 symbols detected. Using GTK+ 2.x and GTK+ 3 in the same pr ...

  7. tcpick

    tcpick 是一款基于文本的嗅探器,能追踪,重组和重排tcp流.

  8. Nginx+Keepalived双主轮询负载均衡

    双主模式使用两个VIP,前段有2台服务器,互为主从,两台服务器同时工作,不存在资源浪费情况.同时在前端的DNS服务器对网站做多条A记录,实现了Nginx的负载均衡,当一台服务器故障时候,资源会转移到另 ...

  9. HDU1195 双向BFS(或BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1195 , 双向BFS或者直接BFS也可以过. 其实这道题只是单向BFS就可以过的,但是为了练算法,所以 ...

  10. 查看SAP CRM和C4C的UI technical信息

    CRM 比如我们想看Quantity这个字段到底是绑在哪个模型上,选中该字段按F2: 就能知道是绑在Context node BTADMINI的QUANTITY字段上. C4C 同理,使用debugM ...