Custom Painter on JProgressBar

匿名 (未验证) 提交于 2019-12-03 08:52:47

问题:

I'm attempting to change the colour of Progress Bars in my current Swing L&F (I'm using Nimbus at the moment) by using a custom Painter object, but when created these Progress Bars sometimes stick with their original colouring (this change seems to occur randomly).

I'm probably missing something simple but I'm stumped, Painter object (and it's invocation below)...

import javax.swing.Painter; import java.awt.*;  public class ProgressPainter implements Painter {  private Color light, dark; private GradientPaint gradPaint;  public ProgressPainter(Color light, Color dark) {     this.light = light;     this.dark = dark; }  @Override public void paint(Graphics2D g, Object c, int w, int h) {     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);     gradPaint = new GradientPaint((w / 2.0f), 0, light, (w / 2.0f), (h /2.0f), dark, true);     g.setPaint(gradPaint);     g.fillRect(2, 2, (w - 5), (h - 5));      Color outline = new Color(0, 85, 0);     g.setColor(outline);     g.drawRect(2, 2, (w - 5), (h - 5));     Color trans = new Color(outline.getRed(), outline.getGreen(), outline.getBlue(), 100);     g.setColor(trans);     g.drawRect(1, 1, (w - 3), (h - 3));  } }

Invoked at application start-up with...

UIManager.put("ProgressBar[Enabled].foregroundPainter", new ProgressPainter(new Color(125, 255, 125), new Color(25, 175, 25))); UIManager.put("ProgressBar[Enabled+Indeterminate].foregroundPainter", new ProgressPainter(new Color(125, 255, 125), new Color(25, 175, 25)));

A simple JProgressBar is then created later using...

    JProgressBar progBar = new JProgressBar(0, 100);     progBar.setStringPainted(true);     progBar.setBounds(20, 10, 260, 30);      frame.add(progBar);     frame.setVisible(true);

回答1:

I had a similar program myself.

Can't remember the actual code though.

but i had to get the UI properties, overwrite the colour and reapply them.

I was using netbeans at the time, and changing the colour field from the properties was not effecting it since the Nimbus UI was set.

Try changing the ui to other ones and see if the colour is allowed to change. I'll search for the code i had used at the time in the mean time.

found it

UIDefaults defaults = UIManager.getLookAndFeelDefaults(); defaults.put("nimbusOrange", new Color(0, 0, 255)); progressBar.putClientProperty("Nimbus.Overrides.InheritDefaults", Boolean.TRUE); progressBar.putClientProperty("Nimbus.Overrides", defaults);

where progressBar is the JProgressBar



转载请标明出处:Custom Painter on JProgressBar
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!