?? 第二十四章例子.txt
字號:
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");
//根據(jù)程序所在的地址處的聲音文件1.au創(chuàng)建音頻對象,Applet類的
// getCodeBase() 方法可以獲得小程序所在的html頁面的URL地址。
button_play=new Button("開始播放");button_loop=new Button("循環(huán)播放");
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();//當(dāng)離開此頁面時停止播放。
}
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);//創(chuàng)建一個新的線程。
thread.setPriority(Thread.MIN_PRIORITY);
button_play=new Button("開始播放"); button_loop=new Button("循環(huán)播放");
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();}//當(dāng)離開此頁面時停止播放。
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中創(chuàng)建音頻對象。
text.setText("請稍等...");
if(clip!=null)
{button_play.setBackground(Color.green);
button_play.setBackground(Color.green);
text.setText("您可以播放了");
}//當(dāng)獲得音頻對象后,通知客戶可以播放了。
}
}
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("循環(huán)播放");
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();//當(dāng)離開此頁面時停止播放。
}
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中創(chuàng)建音頻對象。
text.setText("請稍等...");
if(clip!=null)
{button_play.setBackground(Color.green);button_loop.setBackground(Color.green);
text.setText("您可以播放了");//當(dāng)獲得音頻對象后,通知客戶可以播放了。
}
}
}
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("總數(shù)"));
for(int i=1;i<=N;i++)
{choice.add(getParameter(String.valueOf(i)));
}
button_play=new Button("開始播放"); button_loop=new Button("循環(huán)播放");
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();
//一個串調(diào)用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();//當(dāng)離開此頁面時停止播放。
}
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中創(chuàng)建音頻對象
text.setText("請稍等...");
if(clip!=null)
{button_play.setBackground(Color.green);button_loop.setBackground(Color.green);
text.setText("您可以播放了");
} //當(dāng)獲得音頻對象后,通知客戶可以播放了。
}
}
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);//創(chuàng)建播放器。
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();//媒體預(yù)提取。
}
public void stop()
{if(player!=null)
{ player.stop();player.deallocate();}
}
public synchronized void controllerUpdate(ControllerEvent event)
{ myevent=event;
if(event instanceof RealizeCompleteEvent) //當(dāng)發(fā)生的事件是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("視頻系統(tǒng)");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(); }
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -