Java - 在Mac上更改JProgressBar的颜色(没有UIManager)

时间:2016-10-25 09:44:02

标签: java swing colors uicolor jprogressbar

我想更改JProgressbar的颜色,但它不起作用。我可以使用setBackground更改JProgressBar周围的颜色,但不能更改Bar本身的颜色。然而,在这种情况下,setForeground对我不起作用。 我不使用UiManager或任何其他类似的东西。我只是使用swing和awt。 调试器说Foregroundcolor已经改变,但它在视觉上没有改变。 Mac进度条是否有可能无法直观地改变其前景?

//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

这是窗口的完整源代码

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

(我知道我不需要公开所有内容。)

1 个答案:

答案 0 :(得分:0)

您应该将setStringPainted()设置为true。这应该有用。

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

或者您可以按如下方式使用UIManager(我知道您在没有UIManager的情况下提到过)。但这是一种简单的方法。

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

参见: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