Section 29th (java-development of simple music player)

Source: Internet
Author: User

Package Com.tanzhou.jmf;import Java.applet.*; import java.awt.*; import java.awt.Event.*; import java.io.file;import java.net.malformedurlexception;import java.net.url;import javax.swing.*;//The basic idea of JFrame Java GUI program is based on JFrame, which is the object of window on screen, which can be maximized, minimized and closed. /** * ActionListener is an interface in Java for event handling, inherited from EventListener. ActionListener the Listener interface used to receive operation events. Classes that are interested in handling operation events can implement this interface, and objects created with the class may be registered with the component using the addActionListener method of the component. When an action event occurs, the Actionperformed method of the object is called. */ Public classSoundPlayer extends JFrame implements ActionListener {AudioClip ADC;//declaring an audio clip object ADCURL url;//declaring a resource Locator object URLJLabel lb=NewJLabel ();//Create a Label object lbJLabel la=NewJLabel ();//Create a Label object Lafile file;//declaring file ObjectsString FileName;//declares the filename variable, which is used to record the name of the partyBoolean loop=false;//defines the loop variable to determine if the cycles loop operation is performedJFileChooser Chooser=NewJFileChooser ();//creates a file selector object Chooser//Icon a = new ImageIcon ("C:\\Documents and settings\\administrator\\ Desktop \\image\\ying.jpg");//Create a component pictureJPanel PN1=NewJPanel ();//creates a panel container object with the default department: FlowLayout     Public Static voidMain (string[] args) {NewSoundPlayer ();//Pass the program to the title    }     PublicSoundPlayer () {//This class of constructors, which act as initialization, is equivalent to the Init method of ApplectSuper"Music player");//invokes the constructor of the JFrame, which is used to create a new, initially invisible Frame with the specified captionAddwindowlistener (NewWindowadapter () { Public voidWindowclosing (WindowEvent e) {//the user clicks the Close button on the window's system menu and calls Dispose to execute the windowclosedDispose (); if(ADC! =NULL) {//in the case where the audio clip ADC is not NULL, the sound disappears while the window is closedAdc.stop ();        }            }        }); Jbutton[] bn=Newjbutton[3];//creating an JButton array        /** * FlowLayout (Flow layout) components are aligned from left to right according to the order in which they are joined, and rows are filled to the next line to begin the arrangement BorderLayout (Border layout) container divided into east and west           , South, north, and five regions, where only one component can be placed in each region.           The space of the GridLayout (grid layout) container is divided into the grid area of the MXN column, where only one component can be placed per region. CardLayout (Card layout) is like a stack of cards, each of which corresponds to one component, but only one card at a time can be displayed. For situations where multiple components are prevented in one space GridBagLayout (grid package layout) GridLayout upgrade, components are still placed by row, column, but each component can occupy multiple meshes*/JPanel PN=NewJPanel (NewGridLayout ());//Create a Panel container object PN, the department is: GridLayouticon[] IC=Newicon[3];//Creating an icon array         for(inti =0; i < ic.length; i++) {//assign an image to each icon objectIc[i] =NewImageIcon ("C:\\users\\tony liu\\desktop\\image\\"+ (i +1) +". gif"); } bn[0] =NewJButton ("Start", ic[0]);//Create a Button object with text, with an iconbn[0].addactionlistener ( This);//Adding event listenersbn[1] =NewJButton ("Stop", ic[1]); bn[1].addactionlistener ( This); bn[2] =NewJButton ("Loops", ic[2]); bn[2].addactionlistener ( This); Pn.add (bn[0],0);//Add the Start button to the 1th position of the panel PNPn.add (bn[1],1);//Ibid .Pn.add (bn[2],2);//Ibid .         This. Add (pn, Borderlayout.south);//Adding a panel pn to a frame//        //Create a menu for the playerJMenu Filemenu =NewJMenu ("file"); JMenuItem Openmemuitem=NewJMenuItem ("Open"); Openmemuitem.addactionlistener ( This);        Filemenu.add (Openmemuitem); Filemenu.addseparator ();//Add a split barJMenuItem Exitmemuitem =NewJMenuItem ("Exit"); Exitmemuitem.addactionlistener ( This);        Filemenu.add (Exitmemuitem); JMenuBar MenuBar=NewJMenuBar ();        Menubar.add (Filemenu); String Flag="Welcome to visit";  This. Setframe (flag);//calling the Setframe method         This. Setjmenubar (MenuBar);  This. SetSize ( -, the);//to set the size of a frame         This. setvisible (true);//set frame to visible    }     Public voidactionperformed (ActionEvent e) {if(E.getactioncommand (). Equals ("Exit")) {//If you exit the menu item, close the Windows windowDispose ();//call Dispose to execute windowclosed            return; }        if(E.getactioncommand (). Equals ("Open")) {//If you click the Open menu item, select Music to play the file            intval = Chooser.showopendialog ( This);//receive the return status of a file selector            if(val = = jfilechooser.approve_option) {//If the return status is Approve_optionFile = Chooser.getselectedfile ();//returns the selected fileFileName = File.getname ();//gets the filename of the selected fileString flag ="you are enjoying:"+FileName;  This. Setframe (flag); Try{URL URL=NewURL ("File:"+ File.getpath ());//Create resource LocatorADC = Japplet.newaudioclip (URL);//assigning values to an audio clip object ADCAdc.play ();//start playing this audio clip}Catch(malformedurlexception E1) {System. out. println ("This file cannot be played"); }            }        }        //If the user discards the selection file, returns        if(E.getactioncommand (). Equals ("Start") ) {String flag="you are enjoying:"+FileName; if(ADC = =NULL) {flag="Please select Play file";  This. Setframe (flag); return;            } adc.play ();  This. Setframe (flag); }        if(E.getactioncommand (). Equals ("Stop") ) {adc.stop ();//Stop playing this audio clip. String flag ="Stop playback:"+FileName;  This. Setframe (flag); }        if(E.getactioncommand (). Equals ("Loops") ) {loop= !Loop; String Flag=""; if(Loop) {Adc.play (); Adc.loop ();//start playing this audio clip in a circular fashionFlag ="Loop Playback:"+FileName; } Else{adc.play (); Flag="Sequential playback:"+FileName; }             This. Setframe (flag); }    }    /** * Extract the public part * * * @param flag*/     Public voidsetframe (String flag) {la.settext (flag); //Lb.seticon (a);Pn1.add (LA,0); //Pn1.add (LB, 1);         This. Add (PN1, flowlayout.center); }}

Section 29th (java-development of simple music player)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.