JProgressBar上的自定义画家 [英] Custom Painter on JProgressBar

查看:118
本文介绍了JProgressBar上的自定义画家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用自定义Painter对象来改变当前Swing L& F(我目前正在使用Nimbus)中进度条的颜色,但是在创建这些进度条时,有时会坚持使用原始颜色(这种变化似乎是随机发生的。)

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).

我可能遗漏了一些简单的东西,但我很难过,Painter对象(以及它在下面的调用)...

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

稍后使用...创建一个简单的JProgressBar。

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


推荐答案

我自己有一个类似的程序。

I had a similar program myself.

虽然不记得实际的代码。

Can't remember the actual code though.

但是我必须得到UI属性,覆盖了颜色并重新应用它们。

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

我当时正在使用netbeans,并且自设置Nimbus UI以来,更改属性中的颜色字段并没有影响它。

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.

尝试将ui更改为其他ui并查看是否允许更改颜色。我会搜索我当时使用的代码。

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.

找到它

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

其中progressBar是JProgressBar

where progressBar is the JProgressBar

这篇关于JProgressBar上的自定义画家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆