JProgressBar前景色在JButton点击时没有变化

时间:2016-03-22 00:40:25

标签: java swing jprogressbar

故事:我已经在虚拟宠物模拟器上工作了大约13个小时,我在使用GUI时遇到了这个问题....(就是这样,&#39 ;知道代码的目的)

问题: 我遇到的问题是当我点击" Play"按钮或" Feed"按钮,宠物将更改其统计信息,但JProgressBar颜色不会相应更改,但进度条百分比将。这是应用程序类:

public class App extends JFrame implements ActionListener {

static Pet mainPet = new Pet("Default Pet", 0, 100, 40, 65); //name, age, health, hunger, happy
static JProgressBar health = new JProgressBar(0,100);
static JProgressBar hunger = new JProgressBar(0,100);
static JProgressBar happiness = new JProgressBar(0,100);
static JButton feed = new JButton("Feed");
static JButton play = new JButton("Play");


public App() {
    super("Virtual Pet Simulator");
    setLayout(new FlowLayout());
    health.setStringPainted(true);
    health.setValue(mainPet.getHealth());
    health.setString("Health");
    hunger.setStringPainted(true);
    hunger.setString("Hunger");
    hunger.setValue(mainPet.getHunger());
    happiness.setStringPainted(true);
    happiness.setString("Happiness");
    happiness.setValue(mainPet.getHappiness());
    add(health);
    add(hunger);
    add(happiness);
    add(feed);
    add(play);
    feed.addActionListener(this);
    play.addActionListener(this);
}

public static void main(String[] args) {
    App a = new App();
    a.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    a.pack();
    a.setVisible(true);
    a.setLocationRelativeTo(null);
    Thread updateThread = new Thread() {
        @Override
        public void run() {
            while(true) {
                mainPet.setHappiness(mainPet.getHappiness()-1);
                happiness.setValue(mainPet.happiness);
                if(mainPet.happiness > 75) {
                    happiness.setForeground(Color.green);
                    happiness.setString("Great");
                }
                if(mainPet.happiness <= 75) {
                    happiness.setForeground(Color.cyan);
                    happiness.setString("Good");
                }
                if(mainPet.happiness <= 45) {
                    happiness.setForeground(Color.orange);
                    happiness.setString("Worse");
                }
                if(mainPet.happiness <= 20) {
                    happiness.setForeground(Color.red);
                    happiness.setString("Sad");

                }
                mainPet.setHunger(mainPet.getHunger()-1);
                hunger.setValue(mainPet.hunger);
                if(mainPet.hunger > 75) {
                    hunger.setForeground(Color.green);
                    hunger.setString("Full");
                }
                if(mainPet.hunger >= 75) {
                    hunger.setForeground(Color.cyan);
                    hunger.setString("Good");
                }
                if(mainPet.hunger >= 45) {
                    hunger.setForeground(Color.orange);
                    hunger.setString("Hungry");
                }
                if(mainPet.hunger >= 20) {
                    hunger.setForeground(Color.red);
                    hunger.setString("Starving");

                }
                if(mainPet.hunger <= 0) {
                    hunger.setForeground(Color.magenta);
                    hunger.setString("Extreme hunger!");
                    mainPet.setHealth(mainPet.getHealth()-1);
                }
                health.setValue(mainPet.health);
                if(mainPet.health > 75) {
                    health.setForeground(Color.green);
                    health.setString("Great");
                }
                if(mainPet.health <= 75) {
                    health.setForeground(Color.cyan);
                    health.setString("Good");
                }
                if(mainPet.health <= 45) {
                    health.setForeground(Color.orange);
                    health.setString("Requires attention");
                }
                if(mainPet.health <= 20) {
                    health.setForeground(Color.red);
                    health.setString("Near death");

               }

                if(mainPet.health <= 0) {
                    health.setForeground(Color.magenta);
                    health.setString("Pet death.");
                }
                //a.repaint();
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    };
    updateThread.start();

}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == play) {
        mainPet.setHappiness(mainPet.getHappiness()+1);
        System.out.println(mainPet.getHappiness());
    }
    else if(e.getSource() == feed) {
        mainPet.setHunger(mainPet.getHunger()+1);
        System.out.println(mainPet.getHunger());

    }
}
}

我不会包含Pet类,因为它非常复杂。为了描述它,它是一个包含变量happiness, hunger, health, age, name的类。它还有以下顺序的这些变量的构造函数:String name, int age, int health, int hunger, int happiness,如第三行所述。

回到GUI主题,这是一个截图。正如您所看到的,在IDE中,会​​发出饥饿进度条的级别,并且根据代码,颜色和文本应该会发生变化,但正如您所看到的那样,它不会发生变化。

gui testing

感谢您的帮助。

*我很抱歉,如果这不是很好的质量,我以前做得还不好,而且我不知道我在这里做得这么好。我当然不是最好的编码员。

2 个答案:

答案 0 :(得分:1)

尝试使用

UIManager.put("ProgressBar.foreground", Color.BLUE);

您还可以在新的ProgressBar.background来电中使用“ProgressBar.selectionBackground”,“ProgressBar.selectionForeground”和“UIManager.put()”来更改进度条的其他属性/ p>

答案 1 :(得分:0)

<强>解决方案

我已经用你的建议解决了这个问题(感谢@MadProgrammer指出Swing不是线程安全的)我删除了updateThread并将其移动到main函数中。这确实导致我计划这个程序工作的方式出现问题,但这不适用于这篇文章。

<强>清理

为了清楚起见,我用很多if语句重写了代码的完整部分,并且由于@Matthew指出我的if语句中的某些变量不正确。我还删除了所有static组件,因为@MadProgrammer建议这不是必需的。

感谢您的帮助。

注意: 我希望我在写答案和问题时做得更好,如果你想在评论中批评我,并评论我如何做得更好。尽管如此,这可能也是一种冒犯。