简体   繁体   中英

Java - change colour of JProgressBar on Mac (without UIManager)

I want to change the color of the JProgressbar but it don't work. I can change the Color around the JProgressBar with setBackground, but not the Color of the Bar itself. setForeground is however not working for me in this case. I don't use UiManager or any other kind like that. I just use swing and awt. The debugger said that the Foregroundcolor has changed but it visually doesn't. Is it possible that the Mac progress bar can't change its foreground visually?

//create Progressbar
progBarHeroLife = new JProgressBar(0, Hero.Life.max);
progBarHeroLife.setBounds(25, 676, 200, 50);
progBarHeroLife.setStringPainted(true);
progBarHeroLife.setOpaque(true);
progBarHeroLife.setForeground(Color.orange);
progBarHeroLife.setString(String.valueOf(Hero.Life.actual) + "/" + String.valueOf(Hero.Life.max));
progBarHeroLife.setValue(Hero.Life.actual);

controlePanel.add(progBarHeroLife);
myFrame.add(gamePanel);

Thats how it look like actually

And here is the full source code for the window

package com.company.Game;


import com.company.Characters.Hero;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Window implements ActionListener
{
    public static int scale = 20;

    public static JFrame meinFrame;
    public static int heightMeinFrame = 800;
    public static int heightGamePanel = 500;
    public static int heightControlePanel = 300;
    public static int heightLabelBackground = heightGamePanel;
    public static int widthMeinFrame = 1000;
    public static int widthGamePanel = 1000;
    public static int widthControlePanel = 1000;
    public static int widthLabelBackground = widthGamePanel;

    public static int offsetPlayerY = -(5*scale);

    public static JPanel gamePanel;
    public static JPanel controlePanel;
    public static JLabel labelBackground;
    static ImageIcon backgroundIcon = new ImageIcon("/Users/MJulich/IdeaProjects/Spiel mit Gui/src/Imports/Background.png");
    static JProgressBar progBarHeroLife;
    static JProgressBar progBarHeroMana;
    static JButton myJBtn1;
    static JButton myJBtn2;
    static JButton myJBtn3;
    static JButton myJBtn4;
    static JButton myJBtn5;
    static JButton myJBtn6;
    static JButton myJBtn7;
    static JButton myJBtn8;

    public Window()
    {
        //create Frame
        meinFrame = new JFrame("Mein JFrame Beispiel");
        meinFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        /* Wir setzen die Breite unseres Fensters auf 1000 Pixel
        und die Höhe unseres Fensters auf 800 Pixel */
        meinFrame.setSize(widthMeinFrame, heightMeinFrame);
        meinFrame.setResizable(false);

            //create gamePanel
            gamePanel = new JPanel();
            gamePanel.setLayout(null);
            gamePanel.setBounds(0, 0, widthGamePanel, heightGamePanel);

                //create Background
                backgroundIcon.setImage(backgroundIcon.getImage().getScaledInstance(widthLabelBackground, heightLabelBackground, Image.SCALE_DEFAULT));
                labelBackground = new JLabel(backgroundIcon);
                labelBackground.setOpaque(true);
                labelBackground.setBounds(0,0,widthLabelBackground,heightLabelBackground);
                PaintObjects.paintNew();
                gamePanel.add(labelBackground);


            //create controlePanel
            controlePanel = new JPanel();
            controlePanel.setLayout(null);
            controlePanel.setBounds(0, heightGamePanel+1, widthControlePanel, heightControlePanel);
            controlePanel.setBackground(new Color(200,200,255));

                //create Buttons
                myJBtn1 = new JButton("1");
                myJBtn1.setBounds(25, 526, 200, 50);
                myJBtn2 = new JButton("2");
                myJBtn2.setBounds(275, 526, 200, 50);
                myJBtn3 = new JButton("3");
                myJBtn3.setBounds(525, 526, 200, 50);
                myJBtn4 = new JButton("4");
                myJBtn4.setBounds(775, 526, 200, 50);
                myJBtn5 = new JButton("5");
                myJBtn5.setBounds(25, 601, 200, 50);
                myJBtn6 = new JButton("6");
                myJBtn6.setBounds(275, 601, 200, 50);
                myJBtn7 = new JButton("7");
                myJBtn7.setBounds(525, 601, 200, 50);
                myJBtn8 = new JButton("8");
                myJBtn8.setBounds(775, 601, 200, 50);

                myJBtn1.addActionListener(this);
                myJBtn2.addActionListener(this);
                myJBtn3.addActionListener(this);
                myJBtn4.addActionListener(this);
                myJBtn5.addActionListener(this);
                myJBtn6.addActionListener(this);
                myJBtn7.addActionListener(this);
                myJBtn8.addActionListener(this);

                //create Progressbar
                progBarHeroLife = new JProgressBar(0, Hero.Life.max);
                progBarHeroLife.setBounds(25, 676, 200, 50);
                progBarHeroLife.setStringPainted(true);
                progBarHeroLife.setOpaque(true);
                /**einfärben funktioniert noch nicht*/
                progBarHeroLife.setForeground(Color.yellow);
                progBarHeroLife.setString(String.valueOf(Hero.Life.actual) + "/" + String.valueOf(Hero.Life.max));
                progBarHeroLife.setValue(Hero.Life.actual);

                //add Buttons and Progressbar to controlePanel
                controlePanel.add(myJBtn1);
                controlePanel.add(myJBtn2);
                controlePanel.add(myJBtn3);
                controlePanel.add(myJBtn4);
                controlePanel.add(myJBtn5);
                controlePanel.add(myJBtn6);
                controlePanel.add(myJBtn7);
                controlePanel.add(myJBtn8);
                controlePanel.add(progBarHeroLife);

            //add Panels to Frame
            meinFrame.add(gamePanel);
            meinFrame.add(controlePanel);

        //show Frame
        meinFrame.setVisible(true);
    }

    public void actionPerformed (ActionEvent ae)
    {
        // Die Quelle wird mit getSource() abgefragt und mit den
        // Buttons abgeglichen. Wenn die Quelle des ActionEvents einer
        // der Buttons ist, wird der Text des JLabels entsprechend geändert
        if(ae.getSource() == myJBtn1){
            ButtonListener.buttonPressed(1);
        }
        else if(ae.getSource() == myJBtn2){
            ButtonListener.buttonPressed(2);
        }
        else if(ae.getSource() == myJBtn3){
            ButtonListener.buttonPressed(3);
        }
        else if(ae.getSource() == myJBtn4){
            ButtonListener.buttonPressed(4);
        }
        else if(ae.getSource() == myJBtn5){
            ButtonListener.buttonPressed(5);
        }
        else if(ae.getSource() == myJBtn6){
            ButtonListener.buttonPressed(6);
        }
        else if(ae.getSource() == myJBtn7){
            ButtonListener.buttonPressed(7);
        }
        else if(ae.getSource() == myJBtn8){
            ButtonListener.buttonPressed(8);
        }
    }
}

(I know that i don't need to have everything public.)

You should be set setStringPainted() as true. This should work.

progBarHeroLife.setStringPainted(true);
progBarHeroLife.setForeground(Color.red);
progBarHeroLife.setString("10%");

OR You can use UIManager as follows (I know you have mentioned without UIManager). But that is the easy way.

UIManager.put("ProgressBar.background", Color.BLACK); //colour of the background
UIManager.put("ProgressBar.foreground", Color.RED);  //colour of progress bar
UIManager.put("ProgressBar.selectionBackground",Color.YELLOW);  //colour of percentage counter on black background
UIManager.put("ProgressBar.selectionForeground",Color.BLUE);  //colour of precentage counter on red background

Refer: https://www.daniweb.com/programming/software-development/threads/315100/how-to-change-color-progressbar

https://www.daniweb.com/programming/software-development/threads/315100/how-to-change-color-progressbar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM