java swing定制jprogressbar

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

 

                  

你可能感兴趣的:(java swing定制jprogressbar)