Ok this will be a 2 part question. 1st part: static variables and calling them.
I have 3 classes, the 'shiznatix' class (called first, displays the menu and content), the 'VisualElements' class (builds and returns my menu and content), and the 'ActionHandler' class (handles actions, hopefully).
Now im shiznatix, I have var:
public static String name = "andrew";
and I call it in my shiznatix init method like so:
this.name
no problems. But then I have class VisualElements that has this var:
private static JLabel content;
and I try to call it in one of its methods like this:
this.content = new JLabel(name);
but that gives me the error that I am calling a non-static variable statically. I dont understand the difference or how that is working.
-----------------------
Part 2!
I am trying to change this text in the middle of the applet when they click on a menu item. No luck so far sadly. The only error is that when I compile it, it says that in the ActionHandler class, shiznatix can not be called statically. This is the code for the 3 classes that I have come up with thus far. They are fairly straight forward.
Code: Select all
//shiznatix class
import java.awt.*;
import javax.swing.*;
public class shiznatix extends JApplet
{
public static String name = "andrew";
//main class
public void init()
{
//create the menu
setJMenuBar(VisualElements.CreateMenu());
//set the middle content
add(VisualElements.GetFrameContent(this.name), BorderLayout.CENTER);
}
}
//visual elements class
import javax.swing.*;
public class VisualElements
{
private static JLabel content;
private static JMenuBar menu_bar;
private static JMenu menu_file;
private static JMenuItem menu_file_open;
public static JLabel GetFrameContent(String name)
{
content = new JLabel(name);
content.setHorizontalAlignment(JLabel.CENTER);
return content;
}
public static JMenuBar CreateMenu()
{
menu_bar = new JMenuBar();
menu_file = new JMenu("File");
menu_bar.add(menu_file);
menu_file_open = new JMenuItem("Open");
menu_file_open.addActionListener(new ActionHandler());
menu_file.add(menu_file_open);
return menu_bar;
}
}
//action handler class
import java.awt.event.*;
class ActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//not working!
shiznatix.add(VisualElements.GetFrameContent("wins"));
}
}