gpt4 book ai didi

java - "file", "localhost"在这个程序的构造函数中是什么意思?

转载 作者:行者123 更新时间:2023-12-01 13:40:04 25 4
gpt4 key购买 nike

这似乎是一个愚蠢的问题,但是,在这段代码中我试图制作一个点唱机 GUI。它使用组合框来选择 6 首歌曲。我将 .wav 歌曲文件的副本存储在我的 Music 文件夹和 JukeBox.java 程序所在的文件夹中。我猜“file”是文件名,“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);
}
}

最佳答案

关于构造函数,它构造一个 URL,如下所示:

url1 = new URL ("file", "localhost", "booradleys.wav");

几乎肯定会给您 URL:

file://localhost/booradleys.wav

该错误表明您的内存不足,因此您需要找出导致该问题的原因。我会删除 URL 和音频剪辑(减少到一两个),看看这是否是问题所在,然后尝试使用可能更有效的格式(例如 MP3)或增加运行时环境可用的内存。

关于java - "file", "localhost"在这个程序的构造函数中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20899200/

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