JProgressBar圆角

时间:2016-02-18 20:34:48

标签: java swing jprogressbar

我是java的新手,我正在尝试使用Rounded Corners创建JProgressBar。有人可以帮帮我吗?

我想要一个自定义的ProgressBar。我正在寻找一个圆角和圆形填充的条形图。像这样:

  • 我目前的JProgressBar:

my current JProgressBar

  • 我想要一些这样的:

I Want Some Like This

public class ProgressBarUITest extends JPanel {
 //constructor 
public ProgressBarUITest() {
    JProgressBar jpb = new JProgressBar();
    jpb.setUI(new MyProgressUI());
    jpb.setBackground(new Color(11,14,54));
    jpb.setForeground(new Color(248,248,248,255));
    jpb.setIndeterminate(true);
    jpb.setBorder(BorderFactory.createLoweredBevelBorder());
    this.add(jpb);
}

private static class MyProgressUI extends BasicProgressBarUI {

    @Override
    protected void paintIndeterminate(Graphics g, JComponent c) {

        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(progressBar.getForeground()); 
        g2d.fillRoundRect(boxRect.x, boxRect.y, boxRect.width, boxRect.height, 7, 7);
        }

    @Override
    public Dimension getPreferredSize(JComponent j) {
        return new Dimension(250, 11);

    }
      @Override protected int getBoxLength(int availableLength, int      otherDimension) {
     return (int) (availableLength/4.2);
 }

}

private void display() {
    JFrame f = new JFrame("ProgressBarUITest");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(this);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            new ProgressBarUITest().display();
        }
    });
}
}

我解决了我的问题谢谢大家。

   public class RoundedCornerBorder extends AbstractBorder {
  @Override public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)      {
    Graphics2D g2 = (Graphics2D)g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,               RenderingHints.VALUE_ANTIALIAS_ON);
    int r = height;
    RoundRectangle2D round = new RoundRectangle2D.Float(x, y, width, height, r+5, r+20);
    Container parent = c.getParent();
    if(parent!=null) {
    g2.setColor(new Color(213, 208, 209, 150));
    Area corner = new Area(new Rectangle2D.Float(x, y, width, height));
    corner.subtract(new Area(round));
    g2.fill(corner);
    }
    g2.setColor(Color.BLACK);
    g2.draw(round);
    g2.dispose();
    }
    @Override public Insets getBorderInsets(Component c) {
      return new Insets(3, 8, 2, 8);
    }
     @Override public Insets getBorderInsets(Component c, Insets insets) {
     insets.left = insets.right = 8;
      insets.top = insets.bottom = 2;
     return insets;
     }
     }

  jpb.setBorder(new RoundedCornerBorder());

my current jpb

0 个答案:

没有答案