0
我正在學習在我自己的程序在Java和我試圖使一個健康欄,健康顯示,但當我按下「向上」按鈕(我我正在製作一個簡單的二維遊戲的骨架,該按鈕只是爲了瞭解命令),健康不會下降。當我按下按鈕3次,這說明了我的CMD:Java JProgressbar不更新,未知源錯誤
這裏是我的代碼:
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){
}
}
}
我搜索谷歌和很多上stackoverflow.com,但我沒有」找到任何看起來像我的問題的問題。有人可以幫助我解決這個問題嗎?
你嘗試過使用調試器來找出是什麼happend? – Jens
你期待'main(new String [] {「a」,「b」,「c」});'在你的'ActionListener'中做什麼? – bradimus
請勿張貼堆棧跟蹤的圖片。複製文本並將其粘貼到您的問題中,縮進四個空格,使其顯示爲預先格式化的塊。 (圖片不顯示在文本搜索中,對視力受損的用戶無用。) – VGR