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

📄 j_audio.java

📁 一个十分好的java基础学习的课件
💻 JAVA
字号:
// ////////////////////////////////////////////////////////
// 
// J_Audio.java
// 

// ////////////////////////////////////////////////////////

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class J_Audio extends JApplet implements ActionListener, ItemListener
{
    private AudioClip m_soundFirst,  m_soundSecond, m_soundCurrent;
    private JButton   m_buttonPlay,  m_buttonLoop,  m_buttonStop;
    private JComboBox m_comboChoose;

    public void init( ) // Build interfaces and set sounds
    {
        Container container = getContentPane( );
        container.setLayout( new FlowLayout( ) );

        String choices[] = { "hi", "bark" };
        m_comboChoose = new JComboBox( choices );
        m_comboChoose.addItemListener( this );
        container.add( m_comboChoose );

        m_buttonPlay = new JButton( "Play" );
        m_buttonPlay.addActionListener( this );
        container.add( m_buttonPlay );
        
        m_buttonLoop = new JButton( "Loop" );
        m_buttonLoop.addActionListener( this );
        container.add( m_buttonLoop );
        
        m_buttonStop = new JButton( "Stop" );
        m_buttonStop.addActionListener( this );
        container.add( m_buttonStop );
        
        // load sounds and set m_soundCurrent
        m_soundFirst = getAudioClip( getDocumentBase( ), "hi.au" );
        m_soundSecond = getAudioClip( getDocumentBase( ), "bark.au" );
        m_soundCurrent = m_soundFirst;
    } // End of method: init

    public void stop( ) // Stop playing sound
    {
        m_soundCurrent.stop( );
    }

    public void itemStateChanged( ItemEvent e )
    {
        m_soundCurrent.stop( );
        m_soundCurrent = m_comboChoose.getSelectedIndex( ) == 0 ? m_soundFirst : m_soundSecond;
    } // End of method: itemStateChanged

    public void actionPerformed(ActionEvent e)
    {
        if ( e.getSource( ) == m_buttonPlay ) 
            m_soundCurrent.play( );
        else if ( e.getSource( ) == m_buttonLoop ) 
            m_soundCurrent.loop( );
        else if ( e.getSource( ) == m_buttonStop ) 
            m_soundCurrent.stop( );
    } // End of method: actionPerformed

} // End of class: J_Audio

⌨️ 快捷键说明

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