java swing定制jprogressbar

本文介绍了一个自定义进度条组件的绘制过程,包括如何使用Java Swing创建进度显示效果,通过不同颜色区分进度状态,并实现了带有虚线和边框的进度条样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

final BasicStroke DESH_LINE_STROKE = new BasicStroke(1.5f,
                BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f,
                new float[]{2f}, .0f);
        final int lineSize = 4;

 

setDefaultRenderer(JProgressBar.class, new TableCellRenderer() {

            @Override
            public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, boolean hasFocus, int row, int column) {

                JLabel lbl = new JLabel() {

                    @Override
                    protected void paintComponent(Graphics g) {
                        double process = Double.parseDouble(value.toString());
                        int w = getWidth();
                        double h = 16;
                        double y = (getHeight() - h)/2;
                        double wordWidth = w * 3.0 / 10;
                        Graphics2D g2d = (Graphics2D) g;
                        //画选择后的背景颜色
                        Dimension size = getSize();
                        if (isSelected) {
                            g.setColor(new Color(51, 153, 255));
                            g.fillRect(0, 0, size.width, size.height);
                        }
                        //画文字
                        int n = (int) process;
                        String text = "";
                        if (n == 100) {
                            text = String.valueOf(n);
                        } else if (n < 10) {
                            text = "  " + String.valueOf(n);
                        } else {
                            text = " " + String.valueOf(n);
                        }
                        g2d.setColor(Color.black);
                        g2d.drawString(text, 2, g2d.getFontMetrics().getAscent()+(int)y);
                        double rectWidth = w - wordWidth;
                        double rw = rectWidth / lineSize;
                        //灰色背景
                         if (isSelected) {
                            g.setColor(new Color(51, 153, 255));
                        } else {
                            g2d.setColor(new Color(231, 231, 231));
                        }
                        g2d.fill(new Rectangle2D.Double(wordWidth, y, rectWidth, h));
                        //画进度条
                        g.setColor(new Color(0, 217, 23));
                        if (process > 70) {
                            g.setColor(new Color(255, 201, 0));
                        }
                        if (process > 90) {
                            g.setColor(new Color(186, 12, 12));
                        }
                        w = (int) (process / 100.0 * rectWidth);
                        g2d.fill(new Rectangle2D.Double(wordWidth, y, w, h));
                        //画虚线
                        g2d.setStroke(DESH_LINE_STROKE);
                        g2d.setColor(new Color(152, 152, 152));
                        for (int i = 1; i <= lineSize; i++) {
                            double x = wordWidth + rw * i;
                            Line2D line = new Line2D.Double(x, y, x, h+y);
                            g2d.draw(line);
                        }
                        // 画边框实现
                        g2d.setStroke(new BasicStroke(2.0f));
                        g2d.draw(new Rectangle2D.Double(wordWidth, y, rectWidth-1, h));

                    }

                    @Override
                    public final void update(Graphics g) {
                        this.paintComponent(g);
                    }
                };
                return lbl;
            }
        });

 

                  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值