soundtest.java

来自「java编程开发技巧与实例的编译测试通过的所有例程」· Java 代码 · 共 67 行

JAVA
67
字号
import java.net.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Container;
import java.awt.FlowLayout;
import java.applet.AudioClip;
import javax.swing.*;

public class SoundTest extends JFrame implements ActionListener
{
	private JButton playMusic	=	new JButton("Play Music");
	private JButton loopMusic	=	new JButton("Loop Music");
	private JButton stopMusic	=	new JButton("Stop Music");
	private JButton playSound	=	new JButton("Play Sound");
	private JButton stopSound	=	new JButton("Stop Sound");
	
	private AudioClip	music	=	null,	sound = null;
	public SoundTest()
	{
		super("Sound Test");
		try
		{
			String separator	=	System.getProperty("file.separator");
			String preface		=	"file:" + System.getProperty("user.dir") + separator + "Audio" + separator;
			music	=	Applet.newAudioClip(new URL(preface + "ringin.wav"));
			sound	=	Applet.newAudioClip(new URL(preface + "1631285367444.wav"));
		}
		catch	(MalformedURLException murle)
		{
			System.err.println("Error loading files: " + murle);
		}
		Container content	=	getContentPane();
		content.setLayout(new FlowLayout());
		content.add(playMusic);
		playMusic.addActionListener(this);
		content.add(loopMusic);
		loopMusic.addActionListener(this);
		content.add(stopMusic);
		stopMusic.addActionListener(this);
		content.add(playSound);
		playSound.addActionListener(this);
		content.add(stopSound);
		stopSound.addActionListener(this);
		
		validate();
		pack();
		setVisible(true);
	}
	public void actionPerformed(ActionEvent ae)
	{
		if (ae.getSource() == playMusic)
			music.play();
		else if (ae.getSource() == loopMusic)
			music.loop();
		else if (ae.getSource() == stopMusic)
			music.stop();
		else if (ae.getSource() == playSound)
			sound.play();
		else if (ae.getSource() == stopSound)
			sound.stop();
	}
	public static void main(String args[])
	{
		SoundTest st	=	new SoundTest();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?