1
import
java.awt.BorderLayout;
2
import
java.awt.Color;
3
import
java.awt.Dimension;
4
import
java.awt.FlowLayout;
5
import
java.awt.Graphics;
6
7
import
javax.swing.JFrame;
8
import
javax.swing.JLabel;
9
import
javax.swing.JPanel;
10
11
12
public
class
ColorPregressBar
extends
JLabel
{
13
private
int
minV_
=
0
;
14
private
int
maxV_
=
100
;
15
private
int
value_
=
0
;
16
private
int
colorWidth_
=
0
;
17
private
Color frontColor_
=
new
Color(
150
,
150
,
200
);
18
private
Color belowColor_
=
Color.lightGray;
19
private
String endFix_
=
null
;
20
//
private Dimension preferSize = new Dimension();
21
ColorPregressBar()
{
22
super
();
23
}
24
public
ColorPregressBar(
int
minValue,
int
maxValue)
{
25
setPreferredSize(
new
Dimension(
150
,
20
));
26
if
(minValue
>=
maxValue)
{
27
return
;
28
}
29
minV_
=
minValue;
30
maxV_
=
maxValue;
31
modifyValue();
32
}
33
public
void
setFrontColor(Color frontColor)
{
34
frontColor_
=
frontColor;
35
repaint();
36
}
37
public
void
setBelowColor(Color belowColor)
{
38
belowColor_
=
belowColor;
39
repaint();
40
}
41
public
void
setFix(String str)
{
42
endFix_
=
str;
43
repaint();
44
}
45
public
boolean
setMinMaxValue(
int
minValue,
int
maxValue)
{
46
if
(minValue
>=
maxValue)
{
47
return
false
;
48
}
49
minV_
=
minValue;
50
maxV_
=
maxValue;
51
modifyValue();
52
repaint();
53
return
true
;
54
}
55
private
void
modifyValue()
{
56
if
(value_
<
minV_)
{
57
value_
=
minV_;
58
}
else
if
(value_
>
maxV_)
{
59
value_
=
maxV_;
60
}
61
}
62
protected
void
paintComponent(Graphics g)
{
63
Color colorTemp
=
g.getColor();
64
g.setColor(frontColor_);
65
g.fillRect(
0
,
0
, colorWidth_, getPreferredSize().height);
66
g.setColor(belowColor_);
67
g.fillRect(colorWidth_,
0
, getPreferredSize().width
-
colorWidth_, getPreferredSize().height);
68
g.setColor(colorTemp);
69
super
.paintComponent(g);
70
}
71
public
void
setValue(
int
value)
{
72
if
(value
<
minV_)
{
73
value_
=
minV_;
74
}
else
if
(value
>
maxV_)
{
75
value_
=
maxV_;
76
}
else
{
77
value_
=
value;
78
}
79
colorWidth_
=
(getPreferredSize().width
*
(value_
-
minV_))
/
(maxV_
-
minV_);
80
String txt
=
""
+
value;
81
if
(endFix_
!=
null
)
{
82
txt
+=
endFix_;
83
}
84
setText(txt);
85
}
86
87
public
static
void
main(String[] args)
{
88
JFrame f
=
new
JFrame();
89
f.setSize(
400
,
300
);
90
ColorPregressBar progress
=
new
ColorPregressBar(
-
20
,
100
);
91
progress.setFix(
"
[db]
"
);
92
progress.setValue(
50
);
93
JPanel panel
=
new
JPanel();
94
panel.setLayout(
new
FlowLayout());
95
panel.add(progress);
96
f.getContentPane().add(panel, BorderLayout.CENTER);
97
f.show();
98
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
99
100
}
101
}
102
|
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
24 | 25 | 26 | 27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 |
|
导航
统计
- 随笔: 115
- 文章: 1
- 评论: 86
- 引用: 0
常用链接
留言簿(5)
随笔档案(115)
网址
搜索
积分与排名
最新评论
阅读排行榜
评论排行榜
|
|