Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default How do i call this Applet in this program?

    I am using JOptionPane to make a menu that calls and executes program separately (the applet and menu are not required, i'm just doing it to make it better). I tried to call the init() method but it says "non-static method init() cannot be referenced from a static statement." The applet is used to play a song

    Code:

    First Program: *

    Java Code:
    import javax.swing.JOptionPane;
       public class MexicoProject
       {
           public static void main(String[] args)
          {
            String[] choice = {"History", "Trivia", "Intro", "Anthem", "Quit"};
            String Menu;
      do
            {
                Menu = (String)JOptionPane.showInputDialog(null, "Welcome, this program will teach you about the history of Mexico.\nPick one of the options below.",
                    "Mexico History", JOptionPane.QUESTION_MESSAGE, null, choice, choice[0]);
    
                if (Menu == null)
                    JOptionPane.showMessageDialog(null, "Pick something!");
                else
                {
                    switch (Menu)
                    {
                        case "History":
                            MexicoHistory.History();
                            break;
                        case "Trivia":
                            Quiz();
                            break;
                        case "Intro":
                            FrenchIntro.Intro();
                            break;
                        case "Anthem":
                            MexicoAnthem.Init();
                            break;
                        case "Quit":
                            JOptionPane.showMessageDialog(null, "Goodbye!");
                            break;
                        default:
                            JOptionPane.showMessageDialog(null, "Something went wrong!  Try again!");
                    }
                }   
            } while (Menu != "Quit");
    
        }
        public static void History()
        {
        }
        public static void Quiz()
        {
        }
    Second Program:

    Java Code:
    import java.applet.*;
    
    import java.net.*;
    
    public class MexicoAnthem extends Applet
    
      { 
        Button button;
    
          public void Init()
    
          {
    
                  BorderLayout layout = new BorderLayout();
    
                  setLayout(layout);
    
                  Font font = new Font("TimesRoman", Font.BOLD, 32);
    
                  setFont(font);
    
                  button = new Button("Play Sound");
    
                  add("Center", button);
    
                  resize(250, 250);
    
          }
    
          public boolean action(Event evt, Object arg)
    
          {
    
                  if (evt.target instanceof Button)
    
                  {
    
                        URL codeBase = getCodeBase();
    
                        play(codeBase, "MexicanNationalAnthem.wav");
    
                   }
    
          return true;
    
          }
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,417
    Rep Power
    25

    Default Re: How do i call this Applet in this program?

    non-static method init() cannot be referenced from a static statement.
    non-static methods belong to instances of the class. You need to create an instance of the class and use the reference to that instance of the class to cal the method.
    Java Code:
    TheClass refToClass = new TheClass();  // create an instance
    refToClass.theMethod();    // call a method in the class
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Using j2ee to make phone call applet
    By mr.ab18 in forum Java Applets
    Replies: 1
    Last Post: 08-23-2013, 09:07 AM
  2. Developing java applet for video call
    By usamaalam in forum New To Java
    Replies: 1
    Last Post: 01-16-2013, 12:34 PM
  3. how to call javascript function from java applet?
    By boidi in forum Java Applets
    Replies: 1
    Last Post: 08-31-2012, 09:13 AM
  4. call c++ exe from java applet
    By jojji in forum Java Applets
    Replies: 21
    Last Post: 07-29-2010, 01:47 AM
  5. I need to call and run another program
    By coco in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 05:47 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •