gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:08:42 27 4
gpt4 key购买 nike

我一直在尝试在 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;
}
}
}
}
}
}

最佳答案

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

关于Eclipse Mars 中的 Java 程序立即终止且没有输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34008874/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com