我使用JOptionPane
來製作一個菜單,分別調用和執行程序(小程序和菜單不是必需的,我只是爲了使它更好)。我試圖調用init()
方法,但它說「非靜態方法init()不能從靜態語句中引用」。小程序來播放一首歌曲如何在這個程序中調用這個Applet?
代碼:
的第一個程序: *
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()
{
}
}*
第二個程序:
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;
}
}
「在此程序中調用此小程序」....你真的認爲人們會從你的頭銜中獲得任何有意義的東西嗎? –
這是什麼意思? – user3350704
你的標題是什麼意思?請給出一個有意義的標題,對您的問題進行有意義的總結,例如如何在Swing應用程序中嵌入一個applet;要麼; 「非靜態方法的含義不能從靜態語句引用」等等 –