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):
The rest of the code is as follows (Game.java):
and the file with the Main method in it:
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