PDA

View Full Version : calling a method from a JLabel


raptrex
07-27-2009, 06:13 AM
i have this code
hpLabel = new JLabel("HP:" + hero.gethp() + "/" + hero.getmaxhp());

with gethp() returning an int value
but i get this error

gui.java:38: non-static method gethp() cannot be referenced from a static context
hpLabel = new JLabel("HP:" + hero.gethp() + "/" + hero.getmaxhp());
^
gui.java:38: non-static method getmaxhp() cannot be referenced from a static context
hpLabel = new JLabel("HP:" + hero.gethp() + "/" + hero.getmaxhp());

using jlabel from swing

ckeyrouz
07-27-2009, 04:00 PM
The method gethp() in the instance hero of a class is set as public but not static.

The line of code you have mentioned up there is within a static method or static call, like pubic static something

in this condition you cannot call a non-static method from a static context.

You should remove the static from the method containing the line mentioned above.

If this could not help, post some code to review it.

raptrex
07-27-2009, 11:58 PM
I dont under stand what you mean by making gethp() static but heres my code for the hero class

public class hero
{
private int hp, maxhp, mp, maxmp, att, def, level;

public hero(int hlevel, int hhp, int hmp, int hatt, int hdef)
{
level = hlevel;
hp = hhp;
mp = hmp;
att = hatt;
def = hdef;
}

public void setmaxhp(int hhp)
{
maxhp = hhp;
}
public int getmaxhp()
{
return maxhp;
}
public void sethp(int hhp)
{
hp = hhp;
}
public int gethp()
{
return hp;
}

public void setmaxmp(int hmp)
{
maxmp = hmp;
}
public int getmaxmp()
{
return maxmp;
}
public void setmp(int hmp)
{
mp = hmp;
}
public int getmp()
{
return mp;
}

public void setatt(int hatt)
{
att = hatt;
}
public int getatt()
{
return att;
}
public void setdef(int hdef)
{
def = hdef;
}
public int getdef()
{
return def;
}

public void setlevel(int hlevel)
{
level = hlevel;
}
public int getlevel()
{
return level;
}
}

and my gui class where im displaying the jlabels

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

public class gui
{
JFrame frame;
JLabel hpLabel, mpLabel, attLabel, defLabel;
JTextArea text;

boolean inTown, inForest, inBoss;
hero player = new hero(1,100,50,2,1);

public static void main (String [] args)
{
gui main = new gui();
main.go();
}

public void go()
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JPanel statPanel = new JPanel();
JPanel buttonPanel = new JPanel();

JButton townButton = new JButton("Town");
townButton.addActionListener(new TownListener());
JButton forestButton = new JButton("Forest");
forestButton.addActionListener(new ForestListener());
JButton bossButton = new JButton("Boss");
bossButton.addActionListener(new BossListener());
buttonPanel.add(townButton);
buttonPanel.add(forestButton);
buttonPanel.add(bossButton);

hpLabel = new JLabel("HP:" + hero.gethp() + "/" + hero.getmaxhp());
mpLabel = new JLabel("MP:50/50");
attLabel = new JLabel("ATT:3");
defLabel = new JLabel("DEF:2");

statPanel.setLayout(new BoxLayout(statPanel, BoxLayout.Y_AXIS));
statPanel.add(hpLabel);
statPanel.add(mpLabel);
statPanel.add(attLabel);
statPanel.add(defLabel);

text = new JTextArea(20,35);
text.setLineWrap(true);

JScrollPane scroller = new JScrollPane(text);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scroller);

frame.getContentPane().add(BorderLayout.EAST, statPanel);
frame.getContentPane().add(BorderLayout.CENTER, panel);

frame.getContentPane().add(BorderLayout.SOUTH, buttonPanel);

frame.setSize(550,450);
frame.setVisible(true);
}

class TownListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
text.append("Going to Town \n");
}
}

class ForestListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
text.append("Going to the Forest \n");
}
}

class BossListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
text.append("Going to fight the Boss \n");
}
}
}

Fou-Lu
07-28-2009, 01:06 AM
Change:

hpLabel = new JLabel("HP:" + hero.gethp() + "/" + hero.getmaxhp());

to

hpLabel = new JLabel("HP:" + player.gethp() + "/" + player.getmaxhp());

raptrex
07-28-2009, 01:42 AM
Change:

hpLabel = new JLabel("HP:" + hero.gethp() + "/" + hero.getmaxhp());

to

hpLabel = new JLabel("HP:" + player.gethp() + "/" + player.getmaxhp());



thanks that works

Fou-Lu
07-28-2009, 01:57 AM
You're welcome.
The reason why is because the methods (as previously mentioned) in hero are not listed as static. For this reason, you need to use an instance of the class in order to access them, and to make use of this (which is implicit in this scenario).
Class hero wouldn't make any sense in a static context.