0

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: *

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:

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;
      }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

2

You're trying to call an instance method on a class -- don't do that but instead call it on an instance -- create an object from the class and then call the method.

e.g.,

It's not:

MyClass.someMethod();

but rather

MyClass myInstance = new MyClass();
myInstance.someMethod();

And regardless, this is not how you use applets. Rather they're supposed to be used in an HTML page or with an applet loader. Instead create a JFrame or JDialog and display it.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

How do i call this Applet in this program?

Well, don't do that.

All it does is include a button to play a clip.

A Clip can be played as seen in the Java Sound info. page.

Use a JButton for the button, and add the ActionListener as seen in How to Write an Action Listener.


As an aside. That Applet based code is so old it is using methods deprecated long ago:

I:\projects\numbered\all\AllClasses\src\MexicoAnthem.java:30: warning: 
  [deprecation] action(Event,Object) in Component has been deprecated
      public boolean action(Event evt, Object arg)

The Java Docs go on to mention:

As of JDK version 1.1, should register this component as ActionListener on component which fires action events.

Do not base code on it.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433