如何在不更改外观的情况下为多个JProgressBar分别设置JProgressBar文本颜色 [英] How to set JProgressBar text color independently for multiple JProgressBars without changing Look and Feel

查看:99
本文介绍了如何在不更改外观的情况下为多个JProgressBar分别设置JProgressBar文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续设置JProgressBar文本的颜色希望能够根据进程状态在程序中设置JProgressBar的文本颜色,而不会脱离系统外观.

Continuing from Setting the colors of a JProgressBar text I would like to be able to set the text colors of JProgressBars in my program dependent upon process state and without moving away from System look and feel.

设置JProgressBar文本的颜色中的答案之一,看来我只能设置文本颜色,而不能将UI更改为一组值,因此无论前景和进度如何,我都可以选择看起来不错的中性值,但我想完全自定义JProgressBar

From one of the answers on Setting the colors of a JProgressBar text , it looks like I can only set text colors without changing the UI to a single set of values, so I could choose neutral values that look decent regardless of the foreground and progress, but I would like to fully customize the JProgressBars.

下面是一些具有三种外观的示例代码,试图从好"状态切换到坏"状态.显然,BasicProgressBarUI可以完全切换,但是Metal默认值和System外观不能,它们可以在创建时分配不同的默认值,但是一旦创建,它们似乎是固定的.

Below is some example code with three look and feels trying to switch from "good" to "bad" status. Obviously, the BasicProgressBarUI can be switched over fully, but the Metal default and System look and feel cannot, they can be assigned different default values at creation, but once created, they appear to be fixed.

SSCCE:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;

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;


@SuppressWarnings("serial")
public class ProgressBarTester extends JFrame {

    public ProgressBarTester() {
        super( "Progress Bar Tester" );
        setLayout( new BorderLayout() );

        JPanel goodPanel = new JPanel( new FlowLayout() );
        goodPanel.setLayout( new FlowLayout() );
        JPanel badPanel = new JPanel( new FlowLayout() );
        badPanel.setLayout( new FlowLayout() );

        JProgressBar plainBarG = new JProgressBar();
        plainBarG.setValue( 50 );
        plainBarG.setStringPainted( true );
        plainBarG.setForeground( Color.green.darker() );

        plainBarG.setUI( new BasicProgressBarUI() {

            protected Color getSelectionBackground() {
                return Color.green.darker();
            }
            protected Color getSelectionForeground() {
                return Color.white;
            }
        } );
        goodPanel.add( plainBarG );


        JProgressBar plainBarB = new JProgressBar();
        plainBarB.setValue( 50 );
        plainBarB.setStringPainted( true );
        plainBarB.setForeground( Color.red.darker() );

        plainBarB.setUI( new BasicProgressBarUI() {

            protected Color getSelectionBackground() {
                return Color.red.darker();
            }
            protected Color getSelectionForeground() {
                return Color.black;
            }
        } );
        badPanel.add( plainBarB );

        UIManager.put( "ProgressBar.selectionForeground", Color.white );
        UIManager.put( "ProgressBar.selectionBackground", Color.green.darker() );

        JProgressBar javaDefaultG = new JProgressBar();
        javaDefaultG.setValue( 50 );
        javaDefaultG.setStringPainted( true );
        javaDefaultG.setForeground( Color.green.darker() );
        goodPanel.add( javaDefaultG );

        UIManager.put( "ProgressBar.selectionForeground", Color.black );
        UIManager.put( "ProgressBar.selectionBackground", Color.red.darker() );

        JProgressBar javaDefaultB = new JProgressBar();
        javaDefaultB.setValue( 50 );
        javaDefaultB.setStringPainted( true );
        javaDefaultB.setForeground( Color.red.darker() );
        badPanel.add( javaDefaultB );

        try {
            UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        UIManager.put( "ProgressBar.selectionForeground", Color.white );
        UIManager.put( "ProgressBar.selectionBackground", Color.green.darker() );

        JProgressBar systemG = new JProgressBar();
        systemG.setValue( 50 );
        systemG.setStringPainted( true );
        systemG.setForeground( Color.green.darker() );
        goodPanel.add( systemG );

        UIManager.put( "ProgressBar.selectionForeground", Color.black );
        UIManager.put( "ProgressBar.selectionBackground", Color.red.darker() );

        JProgressBar systemB = new JProgressBar();
        systemB.setValue( 50 );
        systemB.setStringPainted( true );
        systemB.setForeground( Color.red.darker() );
        badPanel.add( systemB );

        this.add( goodPanel, BorderLayout.NORTH );
        this.add( badPanel, BorderLayout.SOUTH );

        pack();
        setVisible( true );

        try {
            Thread.sleep( 2000 );
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        plainBarG.setForeground( Color.red.darker() );
        plainBarG.setUI( new BasicProgressBarUI() {
            protected Color getSelectionBackground() {
                return Color.red.darker();
            }
            protected Color getSelectionForeground() {
                return Color.black;
            }
        } );
        plainBarB.setForeground( Color.green.darker() );
        plainBarB.setUI( new BasicProgressBarUI() {

            protected Color getSelectionBackground() {
                return Color.green.darker();
            }
            protected Color getSelectionForeground() {
                return Color.white;
            }
        } );

        javaDefaultG.setForeground( Color.red.darker() );
        javaDefaultB.setForeground( Color.green.darker() );

        systemG.setForeground( Color.red.darker() );
        systemB.setForeground( Color.green.darker() );
    }

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

}

如SSCCE中所示,可以独立设置文本颜色,但不能动态设置.所有JProgressBar都以某种形式的好"或中立"状态开始,并最终可能变为坏"状态.

As shown in the SSCCE, it is possible to independently set the text colors, but not to do so dynamically. All JProgressBars start with some form of "good" or "neutral" status and may eventually change to a "bad" status.

推荐答案

如您所见,UI委托控制着它如何使用指定的颜色.虽然动态更新颜色从表面上看很吸引人,但它会引起色调和对比度方面的可用性问题.而是考虑添加动态着色的 Border Icon .在此处可以看到前者的一个示例;可以在此处

As you observe, the UI delegate controls how it uses the specified colors. While updating the colors dynamically is superficially appealing, it invites usability problems with respect to hue and contrast. Instead, consider adding a dynamically colored Border or Icon. An example of the former is seen here; an example of the latter is seen here.

这篇关于如何在不更改外观的情况下为多个JProgressBar分别设置JProgressBar文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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