Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
御承扬
java1
提交
4285554f
J
java1
项目概览
御承扬
/
java1
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
java1
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
4285554f
编写于
7月 08, 2019
作者:
御
御承扬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update
上级
a646cb64
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
130 addition
and
1 deletion
+130
-1
src/多线程/SleepMethodTest.java
src/多线程/SleepMethodTest.java
+69
-0
src/多线程/SwingAndThread.java
src/多线程/SwingAndThread.java
+61
-1
未找到文件。
src/多线程/SleepMethodTest.java
0 → 100644
浏览文件 @
4285554f
package
多线程;
//
!/
usr
/
bin
/
env
jdk1
.
8
// encoding:utf-8
//@software:IntelliJ IDEA
//@pack:多线程
//@user:彭友聪
//@date:2019/7/8
//@time:22:04
//@project:IDEA_JAVA
//@file:SleepMethodTest.java
//Author:御承扬
//email:2923616405@qq.com
import
javax.swing.*
;
import
java.awt.*
;
import
java.util.Random
;
public
class
SleepMethodTest
extends
JFrame
{
private
static
final
long
serialVersionUID
=
1L
;
private
Thread
t
;
private
static
Color
[]
color
=
{
Color
.
BLACK
,
Color
.
BLUE
,
Color
.
CYAN
,
Color
.
GREEN
,
Color
.
ORANGE
,
Color
.
YELLOW
};
private
static
final
Random
rand
=
new
Random
(
);
// 创建随机对象
private
static
Color
getC
()
{
// 获取随机颜色值的方法
// 随机产生一个color数组长度
return
color
[
rand
.
nextInt
(
color
.
length
)];
}
private
SleepMethodTest
()
{
t
=
new
Thread
(
new
Draw
()
);
// 创建匿名线程对象
t
.
start
();
}
class
Draw
implements
Runnable
{
// 定义内部类,用来在窗体中绘制线条
int
x
=
30
;
// 定义初识坐标
int
y
=
50
;
@Override
public
void
run
()
{
// 重写线程接口方法
while
(
true
)
{
try
{
Thread
.
sleep
(
100
);
//线程休眠0.1秒
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
// 获取绘图组件上下文对象
Graphics
graphics
=
getGraphics
();
graphics
.
setColor
(
getC
()
);
// 设置绘图颜色
// 绘制直线并递增垂直坐标
graphics
.
drawLine
(
x
,
y
,
100
,
y
);
y
++;
if
(
y
>=
80
)
{
y
=
50
;
}
}
}
}
// 初识化程序界面
public
static
void
init
(
JFrame
frame
,
int
width
,
int
height
)
{
frame
.
setDefaultCloseOperation
(
WindowConstants
.
EXIT_ON_CLOSE
);
frame
.
setSize
(
width
,
height
);
frame
.
setVisible
(
true
);
}
public
static
void
main
(
String
[]
args
)
{
init
(
new
SleepMethodTest
(),
100
,
100
);
}
}
src/多线程/SwingAndThread.java
浏览文件 @
4285554f
...
...
@@ -12,6 +12,66 @@ package 多线程;
//Author:御承扬
//email:2923616405@qq.com
import
java.awt.Container
;
import
java.net.URL
;
import
javax.swing.*
;
public
class
SwingAndThread
{
public
class
SwingAndThread
extends
JFrame
{
private
JLabel
jl
=
new
JLabel
(
);
private
int
count
=
0
;
// 声明计数变量
private
Container
container
;
{
container
=
getContentPane
();
}
private
SwingAndThread
()
{
setBounds
(
300
,
200
,
250
,
100
);
container
.
setLayout
(
null
);
// 使用绝对布局
try
{
URL
url
=
SwingAndThread
.
class
.
getResource
(
"1.gif"
);
Icon
icon
=
new
ImageIcon
(
url
);
jl
.
setIcon
(
icon
);
}
catch
(
NullPointerException
ex
)
{
System
.
out
.
println
(
"图片不存在!!"
);
}
jl
.
setHorizontalAlignment
(
SwingConstants
.
LEFT
);
jl
.
setBounds
(
10
,
10
,
200
,
50
);
jl
.
setOpaque
(
true
);
// 声明线程对象
Thread
t
=
new
Thread
(
new
Roll
()
);
t
.
start
();
container
.
add
(
jl
);
setVisible
(
true
);
setDefaultCloseOperation
(
WindowConstants
.
DISPOSE_ON_CLOSE
);
}
class
Roll
implements
Runnable
{
Roll
()
{
}
@Override
public
final
void
run
()
{
while
(
count
<=
200
)
{
// 将标签的横坐标用变量表示
jl
.
setBounds
(
count
,
10
,
200
,
50
);
try
{
Thread
.
sleep
(
1000
);
// 为了体现滚动效果,使线程休眠 1000 毫秒
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
count
+=
4
;
if
(
count
==
200
)
{
// 当图标到达标签最右边时,使其回到标签最左边
count
=
10
;
}
}
}
}
public
static
void
main
(
String
[]
args
)
{
new
SwingAndThread
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录