Hi
this code is for a subtitle pane in our GUI.
when we try to call
Subtitle.setSubtitleText("whatever");
from another class we get this error:
Game2.java:198: non-static method setSubtitleText(java.lang.String) cannot
be re
ferenced from a static context
Subtitle.setSubtitleText("Woohoo!");
^
1 error
Does anyone know what the problem is? Thanks
Leo
code:
import javax.swing.*;
public class Subtitle extends JPanel {
// Set up the components in the frame.
public Subtitle(int gameNumber, String text){
super();
setLayout(null);
// Create the text label.
labelText = new JLabel("<html><left><font color=white size=-1>" +
text +
"</font></left></html>");
add(labelText);
labelText.setBounds(10, 5, 580, 24);
// Create the subtitle label.
ImageIcon iconSubtitle = Brainbug.createImageIcon("Game" +
gameNumber +
"/Images/Subtitle.jpg", "Subtitle");
JLabel labelSubtitle = new JLabel(iconSubtitle);
add(labelSubtitle);
labelSubtitle.setBounds(0, 0, 600, 36);
}
// Set the text on the text label.
public void setSubtitleText(String text){
labelText.setText("<html><left><font color=white size=-1>" + text
+ "</font></left></html>");
}
// Text label.
private JLabel labelText;
}
Tor Iver Wilhelmsen - 01 Dec 2003 18:14 GMT
> Subtitle.setSubtitleText("whatever");
You need an instance:
Subtitle aSubtitle = new Subtitle();
aSubtitle.setSubtitle("whatever");
Steve Horsley - 01 Dec 2003 21:35 GMT
> Hi
>
[quoted text clipped - 16 lines]
>
> Leo
<snip>
http://www.google.com/search?q=java+non+static+method+static+context
Fahd Shariff - 03 Dec 2003 09:50 GMT
...or change the method into a static method:
public static void setSubtitleText(String text){
labelText.setText("<html><left><font color=white size=-1>" + text
+ "</font></left></html>");
}
u will also need to declare the label static:
// Text label.
private static JLabel labelText;
----
Fahd
Christian Kaufhold - 03 Dec 2003 14:29 GMT
> super();
> setLayout(null);
super(null);
> // Create the text label.
> labelText = new JLabel("<html><left><font color=white size=-1>" +
^^^^^^
???
Christian