⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise23_8.java

📁 java程序设计导论(daniel liang著) 所有偶数课后习题答案
💻 JAVA
字号:
// 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -