2018-04-16 7页 doc 21KB 20阅读
is_321575
暂无简介
Title: 音频播放器
*Description:
*Copyright: Copyright (c) 2003
*Company:
* @author 放飞网 * @version 1.0 */ public class AudioPlay implements Runnable { private JavaSoundAudioClip locate, warning; protected boolean threadExit; protected boolean stopped; protected boolean playing; Thread playerThread; public AudioPlay() { } public void start(File f) { playing = true; stopped = false; try { FileInputStream ff = new FileInputStream(f); locate = new JavaSoundAudioClip(ff); } catch (Exception e) { System.out.println("error"); } playerThread = new Thread(this); playerThread.start(); } public void run() { if (playing) { if (!stopped) { if (locate == null) System.out.print("nulll"); try { locate.play(); System.out.print("test"); } catch (Exception ee) {} } } } public void stop() { stopped = true; threadExit = true; if (playing == true) { playing = false; locate.stop(); } } public void pause() { if (playing == true) { locate.stop(); } } } 3.设计文件过滤FileFilter 我们在使用Windows窗口打开文件时,经常会在文件类型的下拉框中看到多种相应的类型,这样可以限定用户所能够选定的文件类型,也可以提高用户选择文件的速度,因为这样将会只显示相应类型的文件。 而Java中也提供了相应的类可以完成这一功能。 javax.swing.filechooser.FileFilter是一个定义好的抽象类,实现了文件过滤器的基本功能。它只有两个方法:public abstract boolean accept(File f) 和public abstract String getDescription()。由于文件过滤器在我们的程序中只用于Frame窗口中,所以鉴于封装性,我们将其定义为MyFrame的内部类,如下所示: // JScript source code class WavFileFilter extends javax.swing.filechooser.FileFilter { /** * 内部类 * wav文件过滤器,用于打开文件对话框 */ String ext; // 文件扩展名 WavFileFilter(String ext) { this.ext = ext; } /** * 覆盖性方法 * 参数所表示的文件是一个路径名还是一个真实的文件名 * @param file 将给打开的文件 * @return 如果是路径, 返回true, 否则返回false */ public boolean accept(File file) { if (file.isDirectory()) // 是目录,打开 return true; String fileName = file.getName(); int index = fileName.lastIndexOf('.'); if (index > 0 && index < fileName.length() - 1) { // 表示文件名称不为".xxx"与"xxx."之类型 String extension = fileName.substring(index + 1).toLowerCase(); if (extension.equals(ext)) return true; } return false; } /** * 给出对特定文件类型的描述 * @return 对文件类型的描述 */ public String getDescription() { // 返回描述文件的说明字符串 if (ext.equals("wav")) return "Wav File (*.wav)"; return ""; } } } 4.设置成Windows格式 Java具有跨平台的特性,同时造成它的界面也比较独具“个性”。为了使界面更加美观,符合大多数Windows平台使用者的习惯,我们可以在Frame中加上下面一段代码: try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()) ; new Test(); // 初始化所要现实的窗体 } catch(Exception e) { e.printStackTrace(); } 其中,javax.swing.UIManager可以获取和设置外观。 getSystemLookAndFeelClassName()方法会自动过去当前系统平台,在通过setLookAndFeel方法就可以进行设置了。