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

📄

📁 通过实例可以更好的了解java
💻
字号:
24-例子1
import java.applet.*;import java.awt.*;
import java.awt.event.*;
public class Example24_1 extends Applet implements ActionListener
{AudioClip clip;//声明一个音频对象
 Button button_play,button_loop,button_stop;
 public void init()
 {clip=getAudioClip(getCodeBase(),"1.au");
  //根据程序所在的地址处的声音文件1.au创建音频对象,Applet类的
  // getCodeBase() 方法可以获得小程序所在的html页面的URL地址。
  button_play=new Button("开始播放");button_loop=new Button("循环播放");
  button_stop=new Button("停止播放");button_play.addActionListener(this);
  button_stop.addActionListener(this);button_loop.addActionListener(this); 
  add(button_play);add(button_loop);add(button_stop);   
 } 
 public void stop()
 {clip.stop();//当离开此页面时停止播放。
 }
 public void actionPerformed(ActionEvent e)
 {if(e.getSource()==button_play)
   { clip.play();}
  else if(e.getSource()==button_loop)
   { clip.loop();}
  if(e.getSource()==button_stop)
   { clip.stop();} 
 }
}



24-例子2
import java.applet.*;import java.awt.*;
import java.awt.event.*;
public class Example24_2 extends Applet implements ActionListener,Runnable
{AudioClip clip;//声明一个音频对象。
 TextField text;Thread thread;
 Button button_play,button_loop,button_stop;
 public void init()
 { thread=new Thread(this);//创建一个新的线程。
   thread.setPriority(Thread.MIN_PRIORITY);
  button_play=new Button("开始播放"); button_loop=new Button("循环播放");
  button_stop=new Button("停止播放");  text=new TextField(12);
  button_play.addActionListener(this);
  button_stop.addActionListener(this);
  button_loop.addActionListener(this); 
  add(button_play);add(button_loop);add(button_stop);
  add(text);   
 } 
 public void start()
 {thread.start();}
 public void stop()
 {clip.stop();}//当离开此页面时停止播放。
 public void actionPerformed(ActionEvent e)
 {if(e.getSource()==button_play)
   { clip.play();}
  else if(e.getSource()==button_loop)
   { clip.loop();}
  if(e.getSource()==button_stop)
   { clip.stop();} 
 }
 public void run()
 { clip=getAudioClip(getCodeBase(),"1.au");
    //在线程thread中创建音频对象。
   text.setText("请稍等...");
   if(clip!=null)
   {button_play.setBackground(Color.green);
    button_play.setBackground(Color.green);
    text.setText("您可以播放了");
    }//当获得音频对象后,通知客户可以播放了。
 }
}



24-例子3
import java.applet.*;import java.awt.*;
import java.awt.event.*;
public class Example24_3 extends Applet implements ActionListener,Runnable,ItemListener
{AudioClip clip;//声明一个音频对象。
 Choice choice;boolean flag=false;
 TextField text;Thread thread;String s=null;
 Button button_play,button_loop,button_stop;
 public void init()
 {choice=new Choice();
  choice.add("1.au");choice.add("2.au"); choice.add("3.au");choice.add("4.au");  
  button_play=new Button("开始播放"); button_loop=new Button("循环播放");
  button_stop=new Button("停止播放");  text=new TextField(12);
  button_play.addActionListener(this);  button_stop.addActionListener(this);
  button_loop.addActionListener(this);   choice.addItemListener(this);
  add(choice);  add(button_play);add(button_loop);add(button_stop);
  add(text);   
 } 
 public void itemStateChanged(ItemEvent e)
 {if(e.getItemSelectable()==choice)
  { if(choice.getSelectedIndex()==0)
   {s=choice.getSelectedItem(); thread=new Thread(this);
    thread.setPriority(Thread.MIN_PRIORITY); flag=true;this.start();
   }
   else if(choice.getSelectedIndex()==1)
   {s=choice.getSelectedItem(); thread=new Thread(this);
    thread.setPriority(Thread.MIN_PRIORITY);  flag=true;this.start();
   }
   else if(choice.getSelectedIndex()==2)
   {s=choice.getSelectedItem(); thread=new Thread(this);
   thread.setPriority(Thread.MIN_PRIORITY);
    flag=true;this.start();
   }
  else if(choice.getSelectedIndex()==3)
   {s=choice.getSelectedItem(); thread=new Thread(this);
     thread.setPriority(Thread.MIN_PRIORITY); flag=true;this.start();
   }
  }
 }
 public void start()
 {if(flag)
   thread.start(); flag=false;
 }
 public void stop()
 {clip.stop();//当离开此页面时停止播放。
 }
 public void actionPerformed(ActionEvent e)
 {if(e.getSource()==button_play)
   { clip.play();}
  else if(e.getSource()==button_loop)
   { clip.loop();}
  if(e.getSource()==button_stop)
   { clip.stop();} 
 }
 public void run()
 { clip=getAudioClip(getCodeBase(),s);
    //在线程thread中创建音频对象。
   text.setText("请稍等...");
   if(clip!=null)
   {button_play.setBackground(Color.green);button_loop.setBackground(Color.green);
    text.setText("您可以播放了");//当获得音频对象后,通知客户可以播放了。
   }
 }
}



24-例子4
import java.awt.*;
import java.applet.*;
public class Example24_4 extends Applet
{int x=8,y=9;  TextField text;
  public void init()
  { String s1=getParameter("girl");//从html得到"girl"的值(字符串类型)。
   String s2=getParameter("boy");//从html得到"boy"的值(字符串类型)。
   x=Integer.parseInt(s1);  y=Integer.parseInt(s2);  text=new TextField(10);
   text.setText(String.valueOf(x+y)); add(text); 
  } 
}



24-例子5
import java.applet.*;import java.awt.*;import java.awt.event.*;
public class Example24_5 extends Applet implements ActionListener,Runnable,ItemListener
{AudioClip clip;//声明一个音频对象。
 Choice choice;boolean flag=false;
 TextField text;Thread thread;String s=null;
 Button button_play,button_loop,button_stop;
 public void init()
 {choice=new Choice();
  int N=Integer.parseInt(getParameter("总数"));
  for(int i=1;i<=N;i++)
    {choice.add(getParameter(String.valueOf(i)));  
    } 
  button_play=new Button("开始播放");  button_loop=new Button("循环播放");
  button_stop=new Button("停止播放");  text=new TextField(12);
  button_play.addActionListener(this);  button_stop.addActionListener(this);
  button_loop.addActionListener(this);   choice.addItemListener(this);
  add(choice);   add(button_play);add(button_loop);add(button_stop);  add(text);   
 } 
 public void itemStateChanged(ItemEvent e)
 {if(e.getItemSelectable()==choice)
  { if(choice.getSelectedIndex()==0)
   {int t=choice.getSelectedItem().indexOf(":");
    s=(choice.getSelectedItem().substring(t+1)).trim();
     //一个串调用trim()方法可以去掉串的前后空格。
    thread=new Thread(this);thread.setPriority(Thread.MIN_PRIORITY);
    flag=true;this.start();
   }
   else if(choice.getSelectedIndex()==1)
   {int t=choice.getSelectedItem().indexOf(":");
    s=(choice.getSelectedItem().substring(t+1)).trim();
    thread=new Thread(this);thread.setPriority(Thread.MIN_PRIORITY); 
    flag=true;this.start();
   }
   else if(choice.getSelectedIndex()==2)
   {int t=choice.getSelectedItem().indexOf(":");
    s=(choice.getSelectedItem().substring(t+1)).trim();
    thread=new Thread(this);thread.setPriority(Thread.MIN_PRIORITY);
    flag=true;this.start();
   }
  else if(choice.getSelectedIndex()==3)
   {int t=choice.getSelectedItem().indexOf(":");
    s=(choice.getSelectedItem().substring(t+1)).trim();
    thread=new Thread(this);thread.setPriority(Thread.MIN_PRIORITY);
    flag=true;this.start();
   }
  }
 }
 public void start()
 {if(flag)
   thread.start(); flag=false;
 }
 public void stop()
 {clip.stop();//当离开此页面时停止播放。
 }
 public void actionPerformed(ActionEvent e)
 {if(e.getSource()==button_play)
   { clip.play();}
  else if(e.getSource()==button_loop)
   { clip.loop();}
  if(e.getSource()==button_stop)
   { clip.stop();} 
 }
 public void run()
 { clip=getAudioClip(getCodeBase(),s);
    //在线程thread中创建音频对象
   text.setText("请稍等...");
   if(clip!=null)
   {button_play.setBackground(Color.green);button_loop.setBackground(Color.green);
    text.setText("您可以播放了");
   }  //当获得音频对象后,通知客户可以播放了。
 }
}



24-例子6
import java.applet.*;import java.awt.*;
import java.net.*;import java.io.*;
import javax.media.*;
public class Example24_6 extends Applet implements ControllerListener
{ Player player=null;
  Component visualComponent=null;  Component controlComponent=null;
  URL url=null;ControllerEvent myevent=null;
  public void init()
 {try{
        url=new URL(getDocumentBase(),"Music01.mpg");
        player=Manager.createPlayer(url);//创建播放器。
        if(player!=null)
         {player.addControllerListener(this);}
        else
         System.out.println("failed to creat player for"+url);
       }
     catch(MalformedURLException e)
       {System.out.println("URL for Music01.mpg is invalid");}
      catch(IOException e)
       {System.out.println("URL for Music01 is invalid");}
     catch(NoPlayerException e)
       {System.out.println("canot find a player for Music01.mpg");}
 } 
public void start()
 { if(player!=null)
   player.prefetch();//媒体预提取。
 }
public void stop()
 {if(player!=null)
    { player.stop();player.deallocate();}
 }
public synchronized void controllerUpdate(ControllerEvent event) 
 { myevent=event;
  if(event  instanceof  RealizeCompleteEvent) //当发生的事件是RealizeCompleteEvent。
     {if((visualComponent=player.getVisualComponent())!=null)
       {add("Center",visualComponent);}
        if((controlComponent=player.getControlPanelComponent())!=null)
       { if(visualComponent!=null)
             add("South",controlComponent);
         else
              add( "Center",controlComponent);
       }
      validate();//显示这些组件。
     }
  else if(event  instanceof  PrefetchCompleteEvent)
    {    player.start(); 
    }
  }
 }



24-例子7
import java.applet.*;import java.awt.*;import java.net.*;
import java.awt.event.*;import java.io.*;import javax.media.*;
public class E3 extends Applet implements ControllerListener,Runnable,ItemListener
{ Player player=null;String str=null; Thread mythread=null;
  Choice choice;boolean flag=false;  Component visualComponent=null;
  String mediaFile=null;URL   mediaURL=null,codeBase=null;
  Component controlComponent=null;  Component progressBar=null;
  ControllerEvent myevent=null;  Frame frame;
   public void init()
 { str="Music01.MPG"; choice=new Choice();
  choice.add("Music01.MPG");choice.add("Music02.MPG");choice.add("Music03.MPG");
 choice.addItemListener(this); codeBase=getDocumentBase();
 frame=new Frame("视频系统");frame.setBackground(Color.green);
setBackground(Color.red); frame.setSize(660,580);
 frame.addWindowListener(new WindowAdapter()
   {public void windowClosing(WindowEvent e)
     {player.stop();player.deallocate(); frame.setVisible(false);System.exit(0);}  });
      frame.setVisible(false); add(choice);
 } 
public void start()
 { if(flag)
   mythread.start();  flag=false;
 }
public void stop()
 {if(player!=null)
    { player.stop();  }
 }
public void destroy()
 {player.close(); }
public synchronized void controllerUpdate(ControllerEvent event)
 { myevent=event;player.getDuration();
  if(event  instanceof  RealizeCompleteEvent)
    {if((visualComponent=player.getVisualComponent())!=null)
       frame.add("Center",visualComponent);
       if((controlComponent=player.getControlPanelComponent())!=null)
       if(visualComponent!=null)
       frame.add("South",controlComponent);
       else
       frame.add( "Center",controlComponent);
       frame.validate();frame.pack();
    }
   else if(event  instanceof  PrefetchCompleteEvent)
    {  player.start(); 
    }
}
public void itemStateChanged(ItemEvent e)
{if(e.getItemSelectable()==choice)
 {if(choice.getSelectedIndex()==0)
  mythread=null;this.stop(); str=choice.getSelectedItem();
  frame.removeAll();player=null; frame.setVisible(true);
  frame.setBounds(300,100,150,100);frame.pack();
 mythread=new Thread(this);flag=true; this.start();
 }
}
public void run()
{try{mediaURL=new URL(codeBase,str);
        player=Manager.createPlayer(mediaURL);player.getDuration();
        if(player!=null)
         {player.addControllerListener(this);}
        else
         System.out.println("failed to creat player for"+mediaURL);
       }
     catch(MalformedURLException e)
       {System.out.println("URL for"+mediaFile+"is invalid");}
      catch(IOException e)
       {System.out.println("URL for"+mediaFile+"is invalid");}
     catch(NoPlayerException e)
       {System.out.println("canot find a player for"+mediaURL);}
       if(player!=null)
     {   player.prefetch(); }
 }
}

⌨️ 快捷键说明

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