我为我的JProgressBar创建了一个自定义UI,如下所示:

public class BarUI extends BasicProgressBarUI {
    private Rectangle r = new Rectangle();
    @Override
    protected void paintIndeterminate(Graphics g, JComponent c) {
        Graphics2D G2D = (Graphics2D) g;
        G2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        //r = getBox(r);
        //g.setColor(c.getForeground());
        //g.fillRect(r.x,r.y,r.width,r.height);
    }
}


这工作得很好,并将进度条的前景色更改为所需的颜色。问题是我无法更改其背景颜色。
我尝试了以下选项,但是它们都不起作用:
选项1:
c.setBackground(new Color(184, 184, 184,255));
选项2:
((Graphics2D) g).setBackground(new Color(184, 184, 184,255));

为什么这不能正常工作?
更新
我摆弄了更多代码,并从BarUI类中删除了最后三行,因为它们是不需要的。这也是我用来在框架内制作实际进度条的代码:

Bar.setUI(new BarUI());
Bar.setSize(new Dimension(180,2));
Bar.setBackground(new Color(184, 184, 184,255));
Bar.setForeground(new Color(0, 126, 255,255));

最佳答案

它会“显示” opaque的默认JProgressBarfalse,默认情况下使进度条透明

因此,如果我在代码中添加bar.setOpaque(true);,背景将被绘制

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.basic.BasicProgressBarUI;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JProgressBar bar;

        public TestPane() {
            bar = new JProgressBar();
            bar.setIndeterminate(true);
            bar.setOpaque(true);
            bar.setUI(new BarUI());
            bar.setPreferredSize(new Dimension(180, 10));
            bar.setBackground(new Color(184, 184, 184, 255));
            bar.setForeground(new Color(0, 126, 255, 255));
            add(bar);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

    public class BarUI extends BasicProgressBarUI {

        private Rectangle r = new Rectangle();

        @Override
        protected void paintIndeterminate(Graphics g, JComponent c) {
            Graphics2D G2D = (Graphics2D) g;
            G2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                                     RenderingHints.VALUE_ANTIALIAS_ON);
            r = getBox(r);
            g.setColor(c.getForeground());
            g.fillRect(r.x,r.y,r.width,r.height);
        }
    }

}

10-06 10:45