java 自定义进度条_JAVA Swing 自定义进度条样式(简单实现)
本帖最后由 lp563161210 于 2015-8-26 18:44 編輯
效果瀏覽自定義效果:
QQ截圖20150826182536.png (41.28 KB, 下載次數: 88)
5?年前 上傳
java默認效果:
111.png (6.36 KB, 下載次數: 76)
5?年前 上傳
準備工作——什么是JComponent
以下是jdk里對JComponent的描述:
該類是除頂層容器外所有 Swing 組件的基類。要使用繼承自 JComponent 的組件,必須將該組件置于一個根為頂層 Swing 容器的包含層次結構(containment hierarchy)中。頂層 Swing 容器(如 JFrame、JDialog 和 JApplet)是專門為其他 Swing 組件提供繪制自身場所的組件。
JComponent 類提供:
使用 Swing 架構的標準組件和自定義組件的基類。
可由程序員指定,或者由用戶在運行時選擇(可選)的“可插入外觀”(L&F)。每個組件的外觀都由 UI 委托 提供,UI 委托是一個繼承自 ComponentUI 的對象。有關更多信息,請參閱 The Java Tutorial 中的 How to Set the Look and Feel。
全面的鍵擊處理。有關更多信息,請參閱 The Swing Connection 中的文檔 Keyboard Bindings in Swing。
對工具提示的支持:光標停留在組件時所彈出的簡短描述。有關更多信息,請參閱 The Java Tutorial 中的 How to Use Tool Tips。
可訪問性支持。JComponent 包含 Accessible 接口中的所有方法,但是它實際上不實現該接口。由擴展 JComponent 的每個類負責實現該接口。
用于繪制的基礎設施,包括雙緩沖和對邊框的支持。有關更多信息,請參閱 The Java Tutorial 中的 Painting 和 How to Use Borders 章節。
準備工作——為什么不用JLabel或者其他組件來實現?
這個問題很好解答,請朋友們看一下下面的東西,自然就知道了(來自java api)
1.png (9.3 KB, 下載次數: 93)
5?年前 上傳
2.png (8.64 KB, 下載次數: 81)
5?年前 上傳
源碼package com.haozi.UI;
package com.haozi.UI;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import com.haozi.Bean.UrlBean;
public class MyJProgressBar extends JComponent{
private BufferedImage img;
private short img_w;
private short img_h;
private BufferedImage imgbg;
private short imgbg_w;
private short imgbg_h;
private BufferedImage imgend;
private short imgend_w;
private short imgend_h;
private BufferedImage imgl;
private short imgl_w;
private short imgl_h;
private BufferedImage imgr;
private short imgr_w;
private short imgr_h;
/**
* 顯示的最大/小 寬度
*/
private Integer minsize;
private Integer maxsize=880;
private Integer value=1;
/**
* 進度的大小
*/
private int max=100;
public MyJProgressBar(){
super();
LoadImage(new File(UrlBean.progreess_url),new File(UrlBean.progreess_BackGround_url),new File(UrlBean.progreess_end_url)
,new File(UrlBean.progreess_l_url),new File(UrlBean.progreess_r_url));
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
g.drawImage(imgbg, 0, 0, imgbg_w, imgbg_h, null);
for(int i=0;i<=value;i++){
if(i==0){
g.drawImage(imgl, 15, 14, imgl_w, imgl_h, null);
g.drawImage(img, i+22, 14, img_w, img_h, null);
}else if(i==value){
g.drawImage(imgr, value+22, 14, imgr_w, imgr_h, null);
g.drawImage(imgend, (value+22)-(imgend_w-1)/2, 14, imgend_w, imgend_h, null);
}else{
g.drawImage(img, i+22, 14, img_w, img_h, null);
}
}
}
public void LoadImage(File file,File filebg,File fileend,File filel,File filer){
try {
img = ImageIO.read(file);
img_w = (short) img.getWidth();
img_h = (short) img.getHeight();
imgbg = ImageIO.read(filebg);
imgbg_w = (short) imgbg.getWidth();
imgbg_h = (short) imgbg.getHeight();
imgend = ImageIO.read(fileend);
imgend_w = (short) imgend.getWidth();
imgend_h = (short) imgend.getHeight();
imgl = ImageIO.read(filel);
imgl_w = (short) imgl.getWidth();
imgl_h = (short) imgl.getHeight();
imgr = ImageIO.read(filer);
imgr_w = (short) imgr.getWidth();
imgr_h = (short) imgr.getHeight();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public BufferedImage getImg() {
return img;
}
public void setImg(BufferedImage img) {
this.img = img;
}
public short getImg_w() {
return img_w;
}
public void setImg_w(short img_w) {
this.img_w = img_w;
}
public short getImg_h() {
return img_h;
}
public void setImg_h(short img_h) {
this.img_h = img_h;
}
public BufferedImage getImgbg() {
return imgbg;
}
public void setImgbg(BufferedImage imgbg) {
this.imgbg = imgbg;
}
public short getImgbg_w() {
return imgbg_w;
}
public void setImgbg_w(short imgbg_w) {
this.imgbg_w = imgbg_w;
}
public short getImgbg_h() {
return imgbg_h;
}
public void setImgbg_h(short imgbg_h) {
this.imgbg_h = imgbg_h;
}
public BufferedImage getImgend() {
return imgend;
}
public void setImgend(BufferedImage imgend) {
this.imgend = imgend;
}
public short getImgend_w() {
return imgend_w;
}
public void setImgend_w(short imgend_w) {
this.imgend_w = imgend_w;
}
public short getImgend_h() {
return imgend_h;
}
public void setImgend_h(short imgend_h) {
this.imgend_h = imgend_h;
}
public BufferedImage getImgl() {
return imgl;
}
public void setImgl(BufferedImage imgl) {
this.imgl = imgl;
}
public short getImgl_w() {
return imgl_w;
}
public void setImgl_w(short imgl_w) {
this.imgl_w = imgl_w;
}
public short getImgl_h() {
return imgl_h;
}
public void setImgl_h(short imgl_h) {
this.imgl_h = imgl_h;
}
public BufferedImage getImgr() {
return imgr;
}
public void setImgr(BufferedImage imgr) {
this.imgr = imgr;
}
public short getImgr_w() {
return imgr_w;
}
public void setImgr_w(short imgr_w) {
this.imgr_w = imgr_w;
}
public short getImgr_h() {
return imgr_h;
}
public void setImgr_h(short imgr_h) {
this.imgr_h = imgr_h;
}
public Integer getValue() {
return value;
}
public Integer getMinsize() {
return minsize;
}
public void setMinsize(Integer minsize) {
this.minsize = minsize;
}
public Integer getMaxsize() {
return maxsize;
}
public void setMaxsize(Integer maxsize) {
this.maxsize = maxsize;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public void setValue(Integer value) {
if(value==0){
return;
}
double maxx = Double.parseDouble(max+"");
double valuee = Double.parseDouble(value+"");
double maxxx = Double.parseDouble(maxsize+"");
value = (int) (valuee/maxx*maxxx);
if(value<=maxsize){
this.value = value;
repaint();
}
}
}
解釋
重點是public void setValue(Integer value){} 方法,這個方法里要對當前value和最大value,以及顯示的寬度之間進行轉換。
也就是這一行代碼value = (int) (valuee/maxx*maxxx);
其他的其實沒有什么難的。如果想知道上面公式的結果,請自己加輸出語句看看(其實就是求百分比)
結尾
代碼有點亂,是臨時寫的,還沒有整理,大家如果要用,簡化請自己做一下。
資源下載
img.rar
(7.5 KB , 下載次數: 54 )
5?年前 上傳
請點擊文件名下載附件!
總結
以上是生活随笔為你收集整理的java 自定义进度条_JAVA Swing 自定义进度条样式(简单实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 汇编语言全套视频教程
- 下一篇: 黑客攻防之SQL注入原理解析入门教程