Java JProgressbar 不更新,未知来源错误

标签 java jprogressbar

我正在自己学习用java编程,我正在尝试制作一个健康栏,健康状况会显示出来,但是当我按下“向上”按钮时(我正在为一个简单的2d游戏制作一个骨架,该按钮只是为了知道命令)健康状况不会下降。当我按下按钮 3 次时,这会显示在我的 cmd 上:

/image/ydJGJ.png

这是我的代码:

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

public class test{

    private static JFrame frame = new JFrame("test");

    private static JPanel panel = new JPanel();
    private static JPanel panel1 = new JPanel();
    private static JPanel panel2 = new JPanel();

    private static JButton button1 = new JButton("UP");
    private static JButton button2 = new JButton("DN");
    private static JButton button3 = new JButton("L");
    private static JButton button4 = new JButton("R");

    private static JLabel background = new JLabel(new ImageIcon("C:\\Users\\beau\\Downloads\\background.jpg"));

    private static int startHealth = 20;
    private static int healthBoost = 0;
    private static int maxHealth = startHealth + healthBoost;
    private static int damage = 0;
    private static int damageTaken;
    private static int curentHealth;


    public static void main(String[] args){
        //JFrame Properties
        frame.setSize(900,600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);


        //shown stuff Properties and Adding
        damageTaken += damage;
        curentHealth = maxHealth - damageTaken;

        JProgressBar health = new JProgressBar(0,curentHealth);
        health.setStringPainted(true);
        health.setString(curentHealth + "/" + maxHealth);
        health.setValue(curentHealth);

        frame.add(background);

        panel.setLayout(null);
        background.setLayout(null);

        button1.setBounds(61,420,50,50);
        button2.setBounds(61,520,50,50);
        button3.setBounds(11,470,50,50);
        button4.setBounds(111,470,50,50);
        panel.setBounds(725,0,175,600);
        health.setBounds(10,20,150,20);

        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button4);
        panel.add(health);
        background.add(panel);


        button1.addActionListener (new Action1());


        background.revalidate();
    }

    static class Action1 implements ActionListener{
        public void actionPerformed (ActionEvent e){
            damage = 5;
            main(new String[] {"a","b","c"});
        }
    }
    static class Action2 implements ActionListener{
        public void actionPerformed (ActionEvent e){

        }
    }
    static class ActionReset implements ActionListener{
        public void actionPerformed (ActionEvent e){

        }
    }
}
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class test{

    private static JFrame frame = new JFrame("test");

    private static JPanel panel = new JPanel();
    private static JPanel panel1 = new JPanel();
    private static JPanel panel2 = new JPanel();

    private static JButton button1 = new JButton("UP");
    private static JButton button2 = new JButton("DN");
    private static JButton button3 = new JButton("L");
    private static JButton button4 = new JButton("R");

    private static JLabel background = new JLabel(new ImageIcon("C:\\Users\\beau\\Downloads\\background.jpg"));

    private static int startHealth = 20;
    private static int healthBoost = 0;
    private static int maxHealth = startHealth + healthBoost;
    private static int damage = 0;
    private static int damageTaken;
    private static int curentHealth;


    public static void main(String[] args){
        //JFrame Properties
        frame.setSize(900,600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);


        //shown stuff Properties and Adding
        damageTaken += damage;
        curentHealth = maxHealth - damageTaken;

        JProgressBar health = new JProgressBar(0,curentHealth);
        health.setStringPainted(true);
        health.setString(curentHealth + "/" + maxHealth);
        health.setValue(curentHealth);

        frame.add(background);

        panel.setLayout(null);
        background.setLayout(null);

        button1.setBounds(61,420,50,50);
        button2.setBounds(61,520,50,50);
        button3.setBounds(11,470,50,50);
        button4.setBounds(111,470,50,50);
        panel.setBounds(725,0,175,600);
        health.setBounds(10,20,150,20);

        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button4);
        panel.add(health);
        background.add(panel);


        button1.addActionListener (new Action1());


        background.revalidate();
    }

    static class Action1 implements ActionListener{
        public void actionPerformed (ActionEvent e){
            damage = 5;
            main(new String[] {"a","b","c"});
        }
    }
    static class Action2 implements ActionListener{
        public void actionPerformed (ActionEvent e){

        }
    }
    static class ActionReset implements ActionListener{
        public void actionPerformed (ActionEvent e){

        }
    }
}

我在 google 和 stackoverflow.com 上进行了很多搜索,但没有找到任何看起来像我的问题的问题。有人可以帮我解决这个问题吗?

最佳答案

如果最大值低于最小值,您会得到这些异常:

new JProgressBar(0,-5);

查看这段代码:

  /**
     * Initializes value, extent, minimum and maximum. Adjusting is false.
     * Throws an <code>IllegalArgumentException</code> if the following
     * constraints aren't satisfied:
     * <pre>
     * min &lt;= value &lt;= value+extent &lt;= max
     * </pre>
     */
    public DefaultBoundedRangeModel(int value, int extent, int min, int max)
    {
        if ((max >= min) &&
            (value >= min) &&
            ((value + extent) >= value) &&
            ((value + extent) <= max)) {
            this.value = value;
            this.extent = extent;
            this.min = min;
            this.max = max;
        }
        else {
            throw new IllegalArgumentException("invalid range properties");
        }
    }

关于Java JProgressbar 不更新,未知来源错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38699022/

相关文章:

java - 如果选中复选框,则将 JPanel 设置为可见

java - 使用集合类型重载的方法无法按预期工作

java - 如何在我的代码中添加与计时器一起使用的 JProgressBar?

java - 具有双值的 JProgressBar

java - 将 JProgressBar 与下载进程连接

Java 循环未按预期打印结果

java - RecyclerView 不更新他的项目的布局

java - 更改 Swing 进度条的外观

java - Apache HttpClient 4.0.3 - 如何为 POST 请求设置带有 sessionID 的 cookie?

java - 明显移动的 JProgressBar