[英]Need help calling method to class
I can't figure out how to call this class and make it execute. 我不知道如何调用该类并使其执行。 I am calling methods from other classes to make a menu to take it to different related tasks as lesson on Mexico, but I can't figure out how to make it display this class. 我正在从其他类中调用方法来制作菜单,以将其带到墨西哥的课程中,以完成不同的相关任务,但是我不知道如何使它显示此类。 in the different cases each call a method that executes it. 在不同情况下,每个调用一个执行它的方法。 i want to do this for the second program i posted. 我想为我发布的第二个程序执行此操作。 I want to make it execute th program so it can play its song 我想让它执行程序,以便它可以播放歌曲
Main Class: 主类:
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":
{
break;
}
break;
case "Quit":
JOptionPane.showMessageDialog(null, "Goodbye!");
break;
default:
JOptionPane.showMessageDialog(null, "Something went wrong! Try again!");
}
}
} while (Menu != "Quit");
}
Class Being Called: 被调用的类:
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;
public class Song extends JFrame
{
AudioFormat audioFormat;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
boolean stopPlayback = false;
final JButton stopBtn = new JButton("Stop");
final JButton playBtn = new JButton("Play");
final JTextField textField = new JTextField("MexicanNationalAnthem.wav");
public static void main(String args[])
{
new Song();
}//end main
//-------------------------------------------//
public Song() //constructor
{
stopBtn.setEnabled(false);
playBtn.setEnabled(true);
//Instantiate and register action listeners
// on the Play and Stop buttons.
playBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
stopBtn.setEnabled(true);
playBtn.setEnabled(false);
playAudio();//Play the file
}//end actionPerformed
}//end ActionListener
);//end addActionListener()
stopBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//Terminate playback before EOF
stopPlayback = true;
}//end actionPerformed
}//end ActionListener
);//end addActionListener()
getContentPane().add(playBtn,"West");
getContentPane().add(stopBtn,"East");
getContentPane().add(textField,"North");
setTitle("Himno Nacional Mexicano");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(250,70);
setVisible(true);
}//end constructor
//-------------------------------------------//
//This method plays back audio data from an
// audio file whose name is specified in the
// text field.
public void playAudio()
{
try
{
File soundFile = new File(textField.getText());
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
audioFormat = audioInputStream.getFormat();
System.out.println(audioFormat);
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class,audioFormat);
sourceDataLine =(SourceDataLine)AudioSystem.getLine(dataLineInfo);
//Create a thread to play back the data and
// start it running. It will run until the
// end of file, or the Stop button is
// clicked, whichever occurs first.
// Because of the data buffers involved,
// there will normally be a delay between
// the click on the Stop button and the
// actual termination of playback.
new PlayThread().start();//call the PlayThread class
}
catch (Exception e)
{
e.printStackTrace();
System.exit(0);
}//end catch
}//end playAudio
//=============================================//
//Inner class to play back the data from the
// audio file.
class PlayThread extends Thread
{
byte tempBuffer[] = new byte[10000];
public void run()
{
try
{
sourceDataLine.open(audioFormat);
sourceDataLine.start();
int cnt;
//Keep looping until the input read method
// returns -1 for empty stream or the
// user clicks the Stop button causing
// stopPlayback to switch from false to
// true.
while((cnt = audioInputStream.read(tempBuffer,0,tempBuffer.length)) != -1&& stopPlayback == false)
{
if(cnt > 0)
{
//Write data to the internal buffer of
// the data line where it will be
// delivered to the speaker.
sourceDataLine.write(tempBuffer, 0, cnt);
}//end if
}//end while
//Block and wait for internal buffer of the
// data line to empty.
sourceDataLine.drain();
sourceDataLine.close();
//Prepare to playback another file
stopBtn.setEnabled(false);
playBtn.setEnabled(true);
stopPlayback = false;
}
catch (Exception e)
{
e.printStackTrace();
System.exit(0);
}//end catch
}//end run
}//end inner class PlayThread
}//===================================//
Make sure that all the classes remain in same package. 确保所有类都保留在同一程序包中。
You can call the method of other classes by creating a object for class being called .. 您可以通过为名为..的类创建一个对象来调用其他类的方法。
then use the object to call the method name.. 然后使用该对象调用方法名称。
classbeingcalled obj= new classbeingcalled();
obj.methodname();
or declare your method as a static method so you can call the method by using the class name 或将您的方法声明为静态方法,以便您可以使用类名来调用该方法
public static returntype methodname(){
...
}
from any class in the package 来自包装中的任何类别
classname.methodname();
PS: If you are not satisfied with this answer or dint expect this answer please be clear with your question. PS:如果您对这个答案不满意或不满意,请回答这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.