Reputation: 1
When every I click the Charge heal button it doesn't change how many heals I have. When you run the Gui you will see a charge heals button when you click it your heals are supposed to increase.For some reason they won't change though. How do I change that so it changes when I click that button? Here is the Gui:
package Gui;
import Player.Player;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.util.Random;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle.ComponentPlacement;
public class Display {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Display window = new Display();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Display() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void initialize() {
Player playerhp = new Player();
Player playerat = new Player();
Player playerheal = new Player();
Player playerdf = new Player();
Player playerchargeheal = new Player();
String hpdisplay = "Hp:" + playerhp.getplayerhp();
String healdisplay = "Heals:" + playerheal.getplayerheal();
String dfdisplay = "Defense" +playerdf.playerdf;
String Text = "nothing";
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnChargeHeals = new JButton("Charge Heals");//charge heal button
btnChargeHeals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
playerchargeheal.getplayerchargeheal();
String Text = "You charged your heals";
}
});
JLabel lblNewLabel = new JLabel(hpdisplay );
JLabel lblNewLabel_1 = new JLabel(healdisplay);
JLabel lblNewLabel_2 = new JLabel(dfdisplay);
JLabel lblNewLabel_3 = new JLabel(Text);
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(lblNewLabel)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(lblNewLabel_1)
.addGap(80)
.addComponent(lblNewLabel_3))
.addComponent(lblNewLabel_2)
.addComponent(btnChargeHeals))
.addContainerGap(211, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel)
.addComponent(lblNewLabel_1)
.addComponent(lblNewLabel_3))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(lblNewLabel_2)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(btnChargeHeals)
.addContainerGap(182, Short.MAX_VALUE))
);
frame.getContentPane().setLayout(groupLayout);
}
}
Here is the Player:
package Player;
public class Player {
public int playerhp;
public double playerat;
public int playerheal;
public int playerdf;
public int playerchargeheal;
public Player(){
playerhp = 100;
playerat = (int)(Math.random()*10);
playerheal = (10 + (int)(Math.random()*10 -1));
playerdf = (int)(Math.random()*10);
playerchargeheal = (10 + (int)(Math.random()));
}
public int getplayerhp()
{
return playerhp;
}
public double getplayerat()
{
return playerat;
}
public int getplayerheal()
{
playerheal = playerheal -1;
return playerheal;
}
public int getplayerdf()
{
return playerdf;
}
public int getplayerchargeheal()
{
playerheal = playerheal + playerchargeheal;
return playerheal;
}
}
Upvotes: 0
Views: 1317
Reputation: 285405
Your button only charges the heal of a non-displayed PLayer object: playerchargeheal. If you want to change the state of a displayed Player, then the button's ActionListener needs to call the method of the selected displayed Player (though I"m not sure how you select based on your code).
Also note that once you create a String object, the object cannot be changed -- Strings are immutable. If you want to change the display shown by a JLabel, you must specifically do that by calling setText(...)
on the JLabel.
For a simple example:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class FooGui extends JPanel {
private FooPlayer fooPlayer = new FooPlayer();
private JLabel fooPlayerHealth = new JLabel();
private JButton increaseHealthBtn = new JButton("Increase Health");
public FooGui() {
increaseHealthBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// change player's health
fooPlayer.increaseHealth();
// create String to display in JLabel
String text = String.format("Health: %03d", fooPlayer.getHealth());
fooPlayerHealth.setText(text); // change JLabel's text with new information
}
});
String text = String.format("Health: %03d", fooPlayer.getHealth());
fooPlayerHealth.setText(text);
add(fooPlayerHealth);
add(increaseHealthBtn);
}
private static void createAndShowGUI() {
FooGui paintEg = new FooGui();
JFrame frame = new JFrame("FooGui");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(paintEg);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
and
public class FooPlayer {
private static final int INCREASE_AMOUNT = 10;
private int health = 0;
public int getHealth() {
return health;
}
public void increaseHealth() {
health += INCREASE_AMOUNT;
}
public void setHealth(int health) {
this.health = health;
}
}
Upvotes: 1