exercise23_8.java

来自「java程序设计导论(daniel liang著) 所有偶数课后习题答案」· Java 代码 · 共 74 行

JAVA
74
字号
// Exercise23_8.java: Play an audio file selected from a file// dialog boximport java.awt.*;import java.awt.event.*;import java.applet.*;import java.util.*;import javax.swing.*;import java.net.URL;import java.io.*;public class Exercise23_8 extends JFrame implements ActionListener {  private AudioClip audioClip;  private JButton jbtPlay = new JButton("Play");  private JButton jbtLoop = new JButton("Loop");  private JButton jbtStop = new JButton("Stop");  private JButton jbtChooseFile =    new JButton("Choose an Audio File");  private JFileChooser jFileChooser = new JFileChooser();  URL url;  public Exercise23_8() {    url = this.getClass().getResource("/anthem/china.mid");    audioClip = Applet.newAudioClip(url);    JPanel panel = new JPanel();    panel.add(jbtPlay);    panel.add(jbtLoop);    panel.add(jbtStop);    getContentPane().setLayout(new BorderLayout());    getContentPane().add(jbtChooseFile, BorderLayout.NORTH);    getContentPane().add(panel, BorderLayout.SOUTH);    jbtPlay.addActionListener(this);    jbtLoop.addActionListener(this);    jbtStop.addActionListener(this);    jbtChooseFile.addActionListener(this);  }  public void actionPerformed(ActionEvent e) {    String actionCommand = e.getActionCommand();    if (actionCommand.equals("Play")) {      audioClip.play();    }    else if (actionCommand.equals("Loop")) {      audioClip.loop();    }    else if (actionCommand.equals("Stop")) {      audioClip.stop();    }    else if (e.getSource() == jbtChooseFile) {      jFileChooser.setCurrentDirectory(new File("."));      if (jFileChooser.showOpenDialog(this) ==        JFileChooser.APPROVE_OPTION) {        url = this.getClass().getResource(          "anthem/" + jFileChooser.getSelectedFile().getName());        System.out.println("url " + jFileChooser.getSelectedFile().getName());        System.out.println("url " + url);        audioClip = Applet.newAudioClip(url);      }    }  }  /**Main method*/  public static void main(String[] args) {    // Create a frame    Exercise23_8 frame = new Exercise23_8();    frame.setTitle("Exercise23_8");    frame.pack();    frame.setSize(300, 300);    frame.setVisible(true);  }}

⌨️ 快捷键说明

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