EclipseMars中的Java程序,立即终止而没有输出?

EclipseMars中的Java程序,立即终止而没有输出?,java,swing,Java,Swing,我一直在尝试在EclipseMars中编写一个音乐播放器(使用java和JFugue),但是,每次我运行程序时,它都会在几秒钟后终止,并且没有输出。此外,当我进入“运行方式”下拉菜单时,没有任何选项。我觉得这个问题可能非常明显,但我对java有点陌生,所以如果没有错误消息,我就迷路了 这是主课 import java.util.Scanner; import java.awt.*; import java.awt.event.*; import javax.sound.midi.MidiUnav

我一直在尝试在EclipseMars中编写一个音乐播放器(使用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.util.Scanner;
导入java.awt.*;
导入java.awt.event.*;
导入javax.sound.midi.MidiUnavailableException;
导入javax.swing.*;
导入java.io.IOException;
导入org.jfugue.player.player;
导入org.jfugue.pattern.pattern;
公共音乐课
{
公共静态字符串歌曲标题;
公共静态JButton play=新JButton(“play”);
公共静态JButton back=新JButton(“back”);
公共静态JButton pause=新JButton(“pause”);
公共静态JButton forward=新JButton(“forward”);
公共静态标签;
公共静态JComboBox下拉列表;
公共静态JButton repeat=新JButton(“repeat”);
公共静态按钮选择器;
公共静态void main(字符串[]args)
{
//-----------------------------------------------------------------
//启动窗口
JFrame=新的JFrame(“MusicLayer”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的GridLayout(3,1,0,50));
JPanel panelTop=新的JPanel(新的网格布局(2,2,20,50));
添加(新JLabel(“欢迎使用MusicPlayer!”+“\n”+“请选择一首歌曲!”);
字符串[]歌曲={“冬季瓢虫舞”、“歌曲2”、“歌曲3”、“歌曲4”、“歌曲5”、“歌曲6”、“歌曲7”、“歌曲8”、“歌曲9”、“歌曲10”};
dropDown=newjcombobox();
对于(int count1=0;count1<10;count1++)
{
addItem(歌曲[count1]);
}
panelTop.add(下拉菜单);
选择器=新按钮(“选择”);
panelTop.add(选择器);
JPanel panelMiddle=新的JPanel(新的网格布局(2,1,20,50));
面板中间。设置尺寸(500200);
JPanel panelBottom=新的JPanel(新的网格布局(1,4,20,50));
面板底部。设置尺寸(500200);
JPanel panelBottom2=新的JPanel(新的网格布局(1,1,20,50));
面板底部2.设置尺寸(500100);
框架。添加(面板顶部);
框架。添加(面板中间);
框架。添加(面板底部);
框架。添加(2);
//-----------------------------------------------------------------
//设置“选择”按钮的操作(显示歌曲菜单并播放歌曲)
selector.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件)
{
label=新的JLabel();
//使标签更改为歌曲标题
字符串titleOfSong=(字符串)下拉列表;
label.setText(歌曲标题);
panelMiddle.add(标签);
JProgressBar songProgress=新的JProgressBar();
panelMiddle.add(songProgress);//使用计时器显示歌曲的进度,以显示歌曲的持续时间
panelBottom.add(back);//歌曲的数组列表索引-1
panelBottom.add(play);//玩家类
panelBottom.add(暂停);//ManagedPlayer类
panelBottom.add(forward);//歌曲的数组列表索引+1
panelBottom2.add(repeat);//重复播放列表、歌曲或恢复正常
音乐=新音乐(选择器、下拉、后退、播放、暂停、前进、标签、重复、歌曲标题);
尝试
{
音乐。PutSongsToarRayList();
}
捕获(ioe异常ioe)
{
System.out.println(“找不到音乐!”);
}
frame.pack();
音乐。播放音乐(下拉,歌曲标题);
}
});
//-----------------------------------------------------------------------
//导航音乐
play.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件)
{
音乐=新音乐(选择器、下拉、后退、播放、暂停、前进、标签、重复、歌曲标题);
音乐。播放音乐(下拉,歌曲标题);
}
});
pause.addActionListener(新建ActionListener())
{
已执行的公共无效操作(操作事件)
{
尝试
{
音乐=新音乐(选择器、下拉、后退、播放、暂停、前进、标签、重复、歌曲标题);
音乐。暂停音乐(播放);
}
捕获(MidiUnavailableException md)
{
System.out.println(“没有找到歌曲!”);
}
}
});
back.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件)
{
音乐=新音乐(选择器、下拉、后退、播放、暂停、前进、标签、重复、歌曲标题);
音乐的;
}
});
addActionListener(新ActionListener())
{
已执行的公共无效操作(操作事件)
{
音乐=新音乐(选择器、下拉、后退、播放、暂停、前进、标签、重复、歌曲标题);
音乐剧;
}
});
音乐=新音乐(选择器、下拉、后退、播放、暂停、前进、标签、重复、歌曲标题);
音乐的,音乐的;
repeat.addActionListener(新建ActionListener())
{
已执行的公共无效操作(操作事件)
{
音乐=新音乐(选择器、下拉、后退、播放、暂停、前进、标签、重复、歌曲标题);
音乐。设置为重复();
}
});
}
}
这个类提供了导航音乐的方法

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;
                }
            }
        }
    }
}
}
import java.awt.*;
导入javax.swing.*;
导入org.jfugue.player.*;
导入org.jfugue.pattern.*;
导入javax.sound.midi.MidiUnavailableException;
导入java.util.Scanner;
导入java.io.File;
导入java.util.ArrayList;
导入java.io.IOException;
导入java.util.Iterator;
公共音乐课
{
//以后把我不需要的拿出来
私人JButton slctr;
私人JComboBox drpDwn;
私人吉布托