java 自定义进度条_JAVA Swing 自定义进度条样式(简单实现)

本文介绍了如何在JAVA中自定义进度条样式,通过继承JComponent并覆盖paintComponent方法,结合图片实现进度条的定制效果。同时讨论了为何不使用JLabel等组件来实现,并提供了相关代码示例。
摘要由CSDN通过智能技术生成

本帖最后由 lp563161210 于 2015-8-26 18:44 编辑

效果浏览自定义效果:

2884a4702b12722f971d87181db797cd.gif

QQ截图20150826182536.png (41.28 KB, 下载次数: 88)

5 年前 上传

java默认效果:

2884a4702b12722f971d87181db797cd.gif

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)

2884a4702b12722f971d87181db797cd.gif

1.png (9.3 KB, 下载次数: 93)

5 年前 上传

2884a4702b12722f971d87181db797cd.gif

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);

其他的其实没有什么难的。如果想知道上面公式的结果,请自己加输出语句看看(其实就是求百分比)

结尾

代码有点乱,是临时写的,还没有整理,大家如果要用,简化请自己做一下。

资源下载

a4f3080ea060464125ff1e691a572274.png

img.rar

(7.5 KB , 下载次数: 54 )

5 年前 上传

请点击文件名下载附件!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值