Java: Standalone applet

Discussion in 'Mac Programming' started by canada eh, Dec 14, 2011.

  1. macrumors regular

    Joined:
    Dec 22, 2009
    Location:
    Barrie, Ontario
    #1
    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
     
  2. macrumors 6502a

    Joined:
    Jan 15, 2007
    #2
    You use an object called myFrame without declaring it, thats why you are getting the error.
     
  3. thread starter macrumors regular

    Joined:
    Dec 22, 2009
    Location:
    Barrie, Ontario
    #3
    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!
     
  4. macrumors 6502a

    Joined:
    Jan 15, 2007
    #4
    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.
     
  5. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    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.
     

Share This Page