Eclipse Mars中的Java程序立即终止,没有输出?

时间:2015-11-30 22:04:49

标签: java swing

我一直在尝试使用Eclipse Mars中的音乐播放器(使用java和JFugue)进行编码,但是,每次运行程序时,它都会在几秒钟后终止而没有输出。另外,当我去"运行为"下拉菜单,没有选项。我觉得这个问题可能非常明显,但我对java有点新意,所以没有错误信息,我就迷失了。

这是主要的课程

import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.sound.midi.MidiUnavailableException; 
import javax.swing.*;

import java.io.IOException;

import org.jfugue.player.Player;
import org.jfugue.pattern.Pattern;


public class MusicPlayerMain
{
   public static String songTitle;
   public static JButton play = new JButton("play");
public static JButton back = new JButton("back");
public static JButton pause = new JButton("pause");
public static JButton forward = new JButton("forward");
public static JLabel label;
public static JComboBox dropDown;
public static JButton repeat = new JButton("repeat");
public static JButton selector;

public static void main(String[] args)
{
    //-----------------------------------------------------------------
    //starts up window

    JFrame frame = new JFrame ("MusicPlayer");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(3 , 1, 0, 50));

    JPanel panelTop = new JPanel(new GridLayout(2, 2, 20, 50));
    panelTop.add(new JLabel("Welcome to MusicPlayer!" + "\n" + "Please select a song!"));
    String [] songs = {"WinterTime Ladybug Dance", "Song2", "Song3", "Song4", "Song5", "Song6", "Song7", "Song8", "Song9", "Song10"};
    dropDown = new JComboBox();
    for (int count1 = 0; count1 < 10; count1++)
    {
        dropDown.addItem(songs[count1]);
    }
    panelTop.add(dropDown);
    selector = new JButton("Select");
    panelTop.add(selector);


    JPanel panelMiddle = new JPanel(new GridLayout(2, 1, 20, 50));
    panelMiddle.setSize(500,200);

    JPanel panelBottom = new JPanel(new GridLayout(1, 4, 20, 50));
    panelBottom.setSize(500,200);

    JPanel panelBottom2 = new JPanel(new GridLayout(1,1,20,50));
    panelBottom2.setSize(500,100);

    frame.add(panelTop);
    frame.add(panelMiddle);
    frame.add(panelBottom);
    frame.add(panelBottom2);

    //-----------------------------------------------------------------
    //sets an action for the select button (displays song menu and plays song)

    selector.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
    {
        label = new JLabel();
        //make it so that label changes to song title
        String titleOfSong = (String)dropDown.getSelectedItem();
        label.setText(titleOfSong);

        panelMiddle.add(label);
        JProgressBar songProgress = new JProgressBar();
        panelMiddle.add(songProgress); //shows progress of song with timer to show duration of song

        panelBottom.add(back);//the song's array list index -1
        panelBottom.add(play);//a player class
        panelBottom.add(pause);//ManagedPlayer class
        panelBottom.add(forward);//the song's array list index +1

        panelBottom2.add(repeat);//repeats a playlist, song, or returns to normal

        Music musical = new Music(selector, dropDown, back, play, pause, forward, label, repeat, songTitle);

        try
        {
            musical.PutSongsIntoArrayList();
        }
        catch(IOException ioe)
        {
            System.out.println("No music found!");
        }

        frame.pack();

        musical.playTheMusic(dropDown, songTitle);
    }
    });

    //-----------------------------------------------------------------------
    //navigates the music

    play.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
    {
        Music musical = new Music(selector, dropDown, back, play, pause, forward, label, repeat, songTitle);
        musical.playTheMusic(dropDown, songTitle);
    }
    });

    pause.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
    {
        try
    {
        Music musical = new Music(selector, dropDown, back, play, pause, forward, label, repeat, songTitle);

        musical.pauseTheMusic(play);
    }
    catch(MidiUnavailableException md)
    {
        System.out.println("no song found!");
    }
    }
    });

    back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
    {
        Music musical = new Music(selector, dropDown, back, play, pause, forward, label, repeat, songTitle);
        musical.goBack();
    }
    });

    forward.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
    {
        Music musical = new Music(selector, dropDown, back, play, pause, forward, label,repeat,songTitle);
        musical.goForward();
    }
    });

    Music musical = new Music(selector, dropDown, back, play, pause, forward, label, repeat, songTitle);
    musical.keepGoing();

    repeat.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
    {
        Music musical = new Music(selector, dropDown, back, play, pause, forward, label,repeat,songTitle);
        musical.setToRepeat();
    }
    });
}

}

这是提供导航音乐的方法的课程

import java.awt.*;
import javax.swing.*;
import org.jfugue.player.*;
import org.jfugue.pattern.*;
import javax.sound.midi.MidiUnavailableException;
import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;
import java.io.IOException;
import java.util.Iterator;

public class Music
{
 //take out the ones I don't need later
 private JButton slctr;
private JComboBox drpDwn;
private JButton bck;
private JButton ply;
private JButton pse;
private JButton frwrd;
private JButton rpt;
private JLabel lbl;
private String title;
String tokens;

//declaring classes
ArrayList <String> TheTokens = new <String> ArrayList();
File musicFile = new File("Tokens.txt");
Pattern pat = new Pattern(tokens);
Player plays = new Player();

public Music(JButton s, JComboBox d, JButton b, JButton p, JButton ps, JButton f, JLabel l, JButton r, String t) //constructor
{
    slctr = s;
    drpDwn = d;
    bck = b;
    ply = p;
    pse = ps;
    frwrd = f;
    lbl = l;
    rpt = r;
    title = t;
}

public void PutSongsIntoArrayList() throws IOException //puts the song tokens into an ArrayList so it can be manipulated
{
    Scanner fileScanner = new Scanner(musicFile);

    while(fileScanner.hasNext())
    {
        String readIt = fileScanner.nextLine();
        Scanner scanLine = new Scanner(readIt);
        scanLine.useDelimiter(",");
        title = scanLine.next();
        tokens = scanLine.nextLine();
        TheTokens.add(title);
        TheTokens.add(tokens);
        scanLine.close();
    }
    fileScanner.close();
}

public void playTheMusic(JComboBox d, String title)//used for when song is selected, also when song is stopped and must be played again
{ 
    String selectedSong = (String)d.getSelectedItem();
    Iterator itr = TheTokens.iterator();
    while(itr.hasNext())
    {
        if (selectedSong.equals(title))
        {
            plays.play(pat);
        }
    }
}

public void pauseTheMusic(JButton ply) throws MidiUnavailableException //pauses the currently playing song
{
    plays.getManagedPlayer().pause();

    while(!ply.getModel().isPressed()) //allows the method to pause the song indefinitely. Will only end once the song is resumed
    {
        if(ply.getModel().isPressed())
        {
            plays.getManagedPlayer().resume(); //resumes the song
        }
    }
}

public void goBack() //allows the user to listen to the previous song mid-song
{
    int index = TheTokens.indexOf(title)-1;
    TheTokens.get(index);
    plays.play(pat);
}

public void goForward() //allows the user to listen to the next song mid-song
{
    int index = TheTokens.indexOf(title)+1;
    TheTokens.get(index);
    plays.play(pat);
}

public void keepGoing() //causes the next song to play after the first one finishes.
    //NOTE: is similar to musical.goForward() but instead of proceeding to next song when a button is pressed,
    //this method proceeds to the next song after the first song finishes
{
    boolean NextSong = plays.getManagedPlayer().isFinished();
    if(NextSong == true)
    {
        int index = TheTokens.indexOf(title)+1;
        TheTokens.get(index);
        plays.delayPlay(2000, pat);
        plays.play(pat);
    }
}

public void setToRepeat()//cycles through repeat playlist, repeat song, and normal
    //in main class, pressing button will change text on button to show what part of cycle player is in
{
    Iterator itr = TheTokens.iterator();
    while(!rpt.getModel().isPressed())
    {
        if(!itr.hasNext())
        {
            String inDEX = TheTokens.get(0);
        }
        if(rpt.getModel().isPressed())
        {
            while(!rpt.getModel().isPressed())
            {
                pat.repeat(1);
                plays.play(pat);

                if(rpt.getModel().isPressed())
                {
                    break;
                }
            }
        }
    }
}
}

1 个答案:

答案 0 :(得分:2)

尝试在frame.setVisible(true)方法的末尾添加main