appsounddemo.java
字号:
// AppSoundDemo.java
import java.applet.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AppSoundDemo extends JFrame implements ActionListener{
AudioClip audio =null;
JLabel label =new JLabel("File Name");
JTextField field = new JTextField(20);
JButton play =new JButton("Play"),
stop =new JButton("Stop");
String file=null;
Container cp=null;
JPanel p= new JPanel();
public AppSoundDemo(){
cp=getContentPane();
p.add(label);
p.add(field);
p.add(play);
p.add(stop);
cp.add(p,BorderLayout.SOUTH);
play.addActionListener(this);
stop.addActionListener(this);
setSize(400,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public AudioClip getAudio(String file){
URL url = this.getClass().getResource(file);
return Applet.newAudioClip(url);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==play){
file = field.getText();
audio = getAudio(file);
audio.loop();
}else{
audio.stop();
}
}
public static void main(String a[]){
try{
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}catch(Exception e){}
new AppSoundDemo();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -