Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

canada eh

macrumors regular
Original poster
Dec 22, 2009
117
0
Barrie, Ontario
I am trying to get a project set up to start on an applet at school. I copied the code from here and adapted it to fit with two class files I made. but I am getting an error on one line in the first file where it says (line 18):
Code:
add([I]myApplet[/I], BorderLayout.CENTER);

The rest of the code is as follows (Game.java):
Code:
package Game;
import java.awt.*;
import java.awt.event.*;
 public class Game extends Frame implements ActionListener {
  public Game() { // constructor
    super("Hold Applet with Menu"); // define frame title
    // define Menubar
    MenuBar mb = new MenuBar();
    setMenuBar(mb);
    // Define File menu and with Exit menu item
    Menu fileMenu = new Menu("File");
    mb.add(fileMenu);
    MenuItem exitMenuItem = new MenuItem("Exit");
    fileMenu.add(exitMenuItem);
    exitMenuItem.addActionListener (this);
    // define the applet and add to the frame
    game1 myApplet = new game1();
    add(myFrame, BorderLayout.CENTER);
    // call applet's init method (since it is not
    // automatically called in a Java application)
    myApplet.init();
  } // end constructor
  public void actionPerformed(ActionEvent evt) {
    if (evt.getSource() instanceof MenuItem) {
      String menuLabel = ((MenuItem)evt.getSource()).getLabel();
      if(menuLabel.equals("Exit")) {
        // close application, when exit is selected
        dispose();
        System.exit(0);
      } // end if
    } // end if
  } // end ActionPerformed
} // end class

and the file with the Main method in it:
Code:
package Game;
import java.awt.*;
import java.awt.event.*;
 public class Game extends Frame implements ActionListener {
  public Game() { // constructor
    super("Hold Applet with Menu"); // define frame title
    // define Menubar
    MenuBar mb = new MenuBar();
    setMenuBar(mb);
    // Define File menu and with Exit menu item
    Menu fileMenu = new Menu("File");
    mb.add(fileMenu);
    MenuItem exitMenuItem = new MenuItem("Exit");
    fileMenu.add(exitMenuItem);
    exitMenuItem.addActionListener (this);
    // define the applet and add to the frame
    game1 myApplet = new game1();
    add(myFrame, BorderLayout.CENTER);
    // call applet's init method (since it is not
    // automatically called in a Java application)
    myApplet.init();
  } // end constructor
  public void actionPerformed(ActionEvent evt) {
    if (evt.getSource() instanceof MenuItem) {
      String menuLabel = ((MenuItem)evt.getSource()).getLabel();
      if(menuLabel.equals("Exit")) {
        // close application, when exit is selected
        dispose();
        System.exit(0);
      } // end if
    } // end if
  } // end ActionPerformed
} // end class
 
You use an object called myFrame without declaring it, thats why you are getting the error.
 
Ok I think I understand what you mean, I am new to this sort of application. So how would I go about fixing it? Would I need an extra line to declare the frame or is already created and I just have the wrong variable in that spot. Thanks for the help!
 
Ok I think I understand what you mean, I am new to this sort of application. So how would I go about fixing it? Would I need an extra line to declare the frame or is already created and I just have the wrong variable in that spot. Thanks for the help!

I don't know what you are trying to achieve with that line, I guess you need to figure out why you are using it first.
 
Ok I think I understand what you mean, I am new to this sort of application. So how would I go about fixing it? Would I need an extra line to declare the frame or is already created and I just have the wrong variable in that spot. Thanks for the help!

Go back and look at the original code you copied from. Find the myFrame variable. Notice that you don't have that variable in your code. Therefore, you adapted the original code incorrectly.

Also, your original post doesn't show any main() method. You posted the same code twice: the Game class.

You need to focus on details a little better. Things like leaving out variables and posting the wrong code suggest a lack of such focus.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.