Reputation: 137
I am trying to make a program to call my second Java class that consists of music played in the background and a Jframe. The first Jframe has a PLAY button. How do I call another java class completely (with the try and catch, JFrame and such) and close my first Java class?
This is the output of my First Java class
Here is the code of my first Java class.
public class Instruction extends JFrame implements ActionListener {
void introSound(String musicLocation)
{
try {
File musicPath = new File (musicLocation);
if (musicPath.exists()){
AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
Clip clip = AudioSystem.getClip();
clip.open(audioInput);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
else {
System.out.println("Can't find file");
}
}
catch (Exception ex){
ex.printStackTrace();
}
}
//JButton
JButton goNextFrameBtn = new JButton("PLAY");
JFrame insFrame = new JFrame();
Instruction() {prepGUI();}
public void prepGUI(){
insFrame.setTitle("The Phineas and Ferb MATH QUIZ");
insFrame.setSize(1050, 670);
insFrame.setBackground(Color.RED);
insFrame.getContentPane().setLayout(null);
insFrame.setContentPane(new JLabel(new ImageIcon("BG1.JPG")));
insFrame.setResizable(false);
insFrame.setLocationRelativeTo(null);
insFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
insFrame.setVisible(true);
//adding JButton
goNextFrameBtn.setFont(new Font("Ferbtastic", Font.BOLD, 50));
goNextFrameBtn.setBounds(850,520,150,80);
insFrame.add(goNextFrameBtn);
goNextFrameBtn.addActionListener(this);
}
public void actionPerformed (ActionEvent e){
if (e.getSource() == goNextFrameBtn) {
insFrame.dispose();
new PnFMathQuiz();
}
}
public static void main(String[] args) {
String filepath = "PnF Theme Song.wav";
Instruction musicObject = new Instruction();
musicObject.introSound(filepath);
} }
Here is the code of my second Java class that I want to open with the click of the PLAY button
public class PnFMathQuiz extends JFrame{
void playSound(String musicLocation)
{
try {
File musicPath = new File (musicLocation);
if (musicPath.exists()){
AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
Clip clip = AudioSystem.getClip();
clip.open(audioInput);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
else {
System.out.println("Can't find file");
}
}
catch (Exception ex){
ex.printStackTrace();
}
}
JFrame frame = new JFrame();
PnFMathQuiz() {
prepareGUI();
}
public void prepareGUI() {
frame.setTitle("The Phineas and Ferb MATH QUIZ");
frame.setSize(1500, 1000);
frame.getContentPane().setLayout(null);
frame.setContentPane(new JLabel(new ImageIcon("BG2.JPG")));
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
public static void main(String[] args) {
String filepath = "Quirky Worky Song.wav";
PnFMathQuiz musicObject = new PnFMathQuiz();
musicObject.playSound(filepath);
} }
I hope you can help me with this and thank you.
Upvotes: 0
Views: 258
Reputation: 194
You can display the second JFrame first and then dispose the current Jframe. I Removed setVisible(true) from prepareGUI of the Second JFrame. Setting visibility of the next JFrame better be a responsibility of the first JFrame.
public void actionPerformed (ActionEvent e){
if (e.getSource() == goNextFrameBtn) {
JFrame pnMathQuize = new PnFMathQuiz();
pnMathQuize.setVisible(true);
insFrame.dispose();
}
}
And move the things you are doing in the main method of second JFrame to the constructor like this
PnFMathQuiz() {
prepareGUI();
String filepath = "Quirky Worky Song.wav";
this.playSound(filepath);
}
public void prepareGUI() {
frame.setTitle("The Phineas and Ferb MATH QUIZ");
frame.setSize(1500, 1000);
frame.getContentPane().setLayout(null);
frame.setContentPane(new JLabel(new ImageIcon("BG2.JPG")));
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
This should work.
public class Instruction extends JFrame implements ActionListener
{
private Clip clip;
void introSound( String musicLocation )
{
try
{
File musicPath = new File( musicLocation );
if( musicPath.exists() )
{
AudioInputStream audioInput = AudioSystem.getAudioInputStream( musicPath );
clip = AudioSystem.getClip();
clip.open( audioInput );
clip.start();
clip.loop( Clip.LOOP_CONTINUOUSLY );
}
else
{
System.out.println( "Can't find file" );
}
}
catch( Exception ex )
{
ex.printStackTrace();
}
}
//JButton
JButton goNextFrameBtn = new JButton( "PLAY" );
JFrame insFrame = new JFrame();
Instruction()
{
prepGUI();
}
public void prepGUI()
{
insFrame.setTitle( "The Phineas and Ferb MATH QUIZ" );
insFrame.setSize( 1050, 670 );
insFrame.setBackground( Color.RED );
insFrame.getContentPane().setLayout( null );
insFrame.setContentPane( new JLabel( new ImageIcon( "BG1.JPG" ) ) );
insFrame.setResizable( false );
insFrame.setLocationRelativeTo( null );
insFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
insFrame.setVisible( true );
//adding JButton
goNextFrameBtn.setFont( new Font( "Ferbtastic", Font.BOLD, 50 ) );
goNextFrameBtn.setBounds( 850, 520, 150, 80 );
insFrame.add( goNextFrameBtn );
goNextFrameBtn.addActionListener( this );
}
public void actionPerformed( ActionEvent e )
{
if( e.getSource() == goNextFrameBtn )
{
JFrame pnMathQuize = new PnFMathQuiz();
pnMathQuize.setVisible( true );
clip.stop();
insFrame.dispose();
}
}
public static void main( String[] args )
{
String filepath = "PnF Theme Song.wav";
Instruction musicObject = new Instruction();
musicObject.introSound( filepath );
}
}
Upvotes: 1