这可能看起来像一个愚蠢的问题但是,在这段代码中我试图制作一个自动点唱机GUI。它使用组合框来选择6首歌曲。我有.wav歌曲文件的副本存储在我的音乐文件夹和JukeBox.java程序所在的文件夹中。 我猜“文件”是文件名,“localhost”是路径名?无论如何,它目前给我这个错误: - 我将在下面发布完整的程序....
craig@craig-laptop:~/Documents/panda/newGUI$ java JukeBox
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at com.sun.media.sound.JavaSoundAudioClip.readStream(JavaSoundAudioClip.java:345)
at com.sun.media.sound.JavaSoundAudioClip.loadAudioData(JavaSoundAudioClip.java:324)
at com.sun.media.sound.JavaSoundAudioClip.<init>(JavaSoundAudioClip.java:110)
at sun.applet.AppletAudioClip.createAppletAudioClip(AppletAudioClip.java:125)
at sun.applet.AppletAudioClip.<init>(AppletAudioClip.java:66)
at java.applet.Applet.newAudioClip(Applet.java:311)
at JukeBoxControls.<init>(JukeBox.java:36)
at JukeBox.main(JukeBox.java:113)
//JukeBox.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.AudioClip;
import java.net.URL;
class JukeBoxControls extends JPanel {
private JComboBox musicCombo;
private JButton stopButton, playButton;
private AudioClip[] music;
private AudioClip current;
//constructor
public JukeBoxControls () {
URL url1, url2, url3, url4, url5, url6;
url1 = url2 = url3 = url4 = url5 = url6 = null;
//obtain and store the audio clips
try {
url1 = new URL ("file", "localhost", "booradleys.wav");
url2 = new URL ("file", "localhost", "notobig.wav");
url3 = new URL ("file", "localhost", "roots.wav");
url4 = new URL ("file", "localhost", "snakadaktal.wav");
url5 = new URL ("file", "localhost", "sumthing.wav");
url6 = new URL ("file", "localhost", "radiocontrol.wav");
}
catch (Exception exception) {}
music = new AudioClip[7];
music[0] = null; //corresponds to 'make a selection'
music[1] = JApplet.newAudioClip (url1);
music[2] = JApplet.newAudioClip (url2);
music[3] = JApplet.newAudioClip (url3);
music[4] = JApplet.newAudioClip (url4);
music[5] = JApplet.newAudioClip (url5);
music[6] = JApplet.newAudioClip (url6);
JLabel titleLabel = new JLabel ("Java Juke Box");
titleLabel.setAlignmentX (Component.CENTER_ALIGNMENT);
//create the list of strings for combo box
String[] musicNames = {"Make a selection...", "booradleys", "notobig",
"roots", "snakadaktal", "sumthing"};
JComboBox <String> musicCombo = new JComboBox<> (musicNames);
musicCombo.setAlignmentX (Component.CENTER_ALIGNMENT);
//set up the buttons
playButton = new JButton ("Play", new ImageIcon ("play.gif"));
playButton.setBackground (Color.white);
playButton.setMnemonic ('p');
stopButton = new JButton ("Stop", new ImageIcon ("stop.gif"));
stopButton.setBackground (Color.white);
stopButton.setMnemonic ('s');
JPanel buttons = new JPanel();
buttons.setLayout (new BoxLayout (buttons, BoxLayout.X_AXIS));
buttons.add (playButton);
buttons.add (Box.createRigidArea (new Dimension(5, 0)));
buttons.add (stopButton);
buttons.setBackground (Color.cyan);
//set up this panel
setPreferredSize (new Dimension (300, 100));
setBackground (Color.cyan);
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
add (Box.createRigidArea (new Dimension(0, 5)));
add (titleLabel);
add (Box.createRigidArea (new Dimension(0, 5)));
add (musicCombo);
add (Box.createRigidArea (new Dimension(0, 5)));
add (buttons);
add (Box.createRigidArea (new Dimension(0, 5)));
musicCombo.addActionListener (new ComboListener());
stopButton.addActionListener (new ButtonListener());
playButton.addActionListener (new ButtonListener());
current = null;
}
//represents the action listener for combo box
private class ComboListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
if (current != null)
current.stop();
current = music[musicCombo.getSelectedIndex()];
}
}
private class ButtonListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
if (current != null)
current.stop();
if (event.getSource() == playButton)
if (current != null)
current.play();
}
}
}
public class JukeBox {
public static void main (String[] args) {
JFrame frame = new JFrame ("Java Juke Box");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JukeBoxControls controlPanel = new JukeBoxControls();
frame.getContentPane().add(controlPanel);
frame.pack();
frame.setVisible(true);
}
}
答案 0 :(得分:1)
关于构造函数,它正在构建一个URL:
url1 = new URL ("file", "localhost", "booradleys.wav");
几乎肯定会给你一个网址:
file://localhost/booradleys.wav
该错误表明您的内存不足,因此您需要弄清楚导致内存的原因。我会删除URL和音频片段(下至一两个)以查看问题是什么,然后使用可能更高效的格式(例如,MP3)或增加运行时环境可用的内存。< / p>