Я сделал простую игру с графическим интерфейсом на Java.Это RPG-игра, и где-то в интерфейсе есть индикатор выполнения, который является индикатором жизни, а внутри него есть другой элемент графического интерфейса, текст, который является значением жизни персонажа.
Сначала все работало нормально, но я добавил ActionerListener
к одной кнопке атаки, и он делает это, уменьшая полосу жизни и ценность жизни (это текст), как настоящая атакана играх делает.Но когда я это делаю, у меня возникает ошибка.
Иногда полоса жизни остается впереди жизненной ценности, а иногда нет, это похоже на случайность.Итак, мой вопрос: есть ли способ установить элемент Gui всегда впереди?В этом случае ценность жизни.
Obs: У меня есть абстрактный класс (CharactersInterface
), где я объявляю все вещи и врагов Орков.
public class wizardInterface extends CharactersInterface{
//interface
public wizardInterface() {
//your name
you = new JLabel(wizard.name);
you.setBounds(85, 20, 160, 30);
panel.add(you);
//hit points
yhitpoints = new JLabel(wizard.hp + "/25");
yhitpoints.setBounds(95, 51, 50, 15);
panel.add(yhitpoints);
//life bar
ylife.setValue(wizard.hp*4);
ylife.setBounds(50, 50, 125, 18);
ylife.setForeground(Color.GREEN);
panel.add(ylife);
//your image
image1 = new ImageIcon(getClass().getResource("wizard.png"));
img1 = new JLabel(image1);
img1.setBounds(50, 80, 128, 128);
panel.add(img1);
//text
text = new JTextArea("Choose your spell!");
text.setFont(new Font("Serif", Font.PLAIN, 16));
text.setBackground(null);
text.setBounds(185, 300, 160, 45);
panel.add(text);
//thunder button
Icon thunder = new ImageIcon(getClass().getResource("thunder.png"));
attack1 = new JButton("Thunder", thunder);
attack1.setBounds(20, 350, 230, 140);
panel.add(attack1);
//fire button
Icon fire = new ImageIcon(getClass().getResource("fire.png"));
attack2 = new JButton("Fire", fire);
attack2.setBounds(280, 350, 230, 140);
panel.add(attack2);
//background image
backgroundImage = new ImageIcon(getClass().getResource("background1.png"));
background = new JLabel(backgroundImage);
background.setBounds(0, -20, 535, 300);
panel.add(background);
//do something when press the button
thehandler handler = new thehandler();
attack1.addActionListener(handler);
attack2.addActionListener(handler);
}
private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent event) {
if(event.getSource()==attack1){
orc.hp -= 1 + dice.nextInt(10) + wizard.intelligence; // 1d10 +2;
}else if(event.getSource()==attack2){
orc.hp -= 3 + dice.nextInt(8) + wizard.intelligence; // 1d8 +4
}
//here is where I reduce the life of the enemy
elife.setValue(orc.hp*2);
ehitpoints.setText(orc.hp + "/50");
if(orc.hp <=0){
System.out.println("you won");
System.exit(0);
}
//orc attacks
wizard.hp -= 1 + dice.nextInt(4) + orc.strong; //1d4 + 2
ylife.setValue(wizard.hp*4);
yhitpoints.setText(wizard.hp + "/25");
if(wizard.hp <=0){
System.out.println("you lose");
System.exit(0);
}
}
}