PDA

View Full Version : Java: Standalone applet




canada eh
Dec 14, 2011, 07:46 PM
I am trying to get a project set up to start on an applet at school. I copied the code from here (http://www.woodger.ca/jv_appst.htm) 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):
add(myApplet, BorderLayout.CENTER);

The rest of the code is as follows (Game.java):
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:
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



foidulus
Dec 14, 2011, 08:22 PM
You use an object called myFrame without declaring it, thats why you are getting the error.

canada eh
Dec 14, 2011, 09:00 PM
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!

foidulus
Dec 15, 2011, 01:04 AM
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.

chown33
Dec 15, 2011, 10:45 AM
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 (http://www.woodger.ca/jv_appst.htm) 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.