该程序可以通过调用main来显示和映像,并且可以在运行applet时播放音频文件。有没有办法同时做这两件事?
public class DisplayWinner extends JApplet {
public static void main(String[] args) throws IOException {
ArrayList<String> images = new ArrayList<String>();
int temp = (int)(Math.random()*2);
String i1 ="0.png";
String i2 ="1.jpg";
images.add(i1);
images.add(i2);
JFrame frame=new JFrame();
BufferedImage img = ImageIO.read(new File(images.get(temp)));
ImageIcon icon = new ImageIcon(img);
JLabel label = new JLabel(icon);
frame.add(label);
frame.setExtendedState( frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
public void init(){
AudioClip ac = getAudioClip(getCodeBase(), "39602- John_Cena_(ShortMp3.com).wav");
ac.play();
}
}
答案 0 :(得分:2)
这就是我在init方法中所拥有的:
public void init() {
ArrayList<String> images = new ArrayList<String>();
int temp = (int)(Math.random()*2);
String i1 ="bm-0.png";
String i2 ="bm-1.jpg";
images.add(i1);
images.add(i2);
img=getImage(getDocumentBase(), "./"+images.get(temp));
mt=new MediaTracker(this);
mt.addImage(img, 0);
try{
mt.waitForID(0);
}
catch(InterruptedException e) { e.printStackTrace(); }
try {
AudioClip ac = getAudioClip(getCodeBase(), "e-20.wav");
ac.play();
}
catch(Exception e) { e.printStackTrace(); }
}
并在paint方法中
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
}