首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

java小程序,但是音乐会突然停止,想要一直播放怎么处理

2012-03-31 
java小程序,但是音乐会突然停止,想要一直播放怎么办?各位高手,这是我做的一个温度转换器,可是根据输入的温

java小程序,但是音乐会突然停止,想要一直播放怎么办?
各位高手,这是我做的一个温度转换器,可是根据输入的温度切换图片,一换音乐也跟着停止了,大家知道怎么才能让音乐一直播放吗?谢谢了
import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*;  
import java.io.*;  
import java.applet.Applet;  
import java.awt.Frame;  
import java.net.URL;  
import javax.swing.JPanel;
import java.applet.AudioClip;



//JButton,JTextField,JLabel的用法  
public class CelsiusConverter implements ActionListener {  
JFrame con;  
JPanel Pl,P2;  
JLabel background ;
JTextField txt,txt2;  
JLabel Label1,Label2,Label3,Label4;  
JButton niu,niu2;  
String music = "白日夢鋼琴曲.wav";  
//加了一个变量
int i=0;
//构造函数  
public CelsiusConverter() {  
//创建容器  
con = new JFrame("温度转换器");  
con.setSize(1000,800);  
Pl = new JPanel();
P2 = new JPanel();
Pl.setLayout(new FlowLayout());
addWidgets_1();
P2.setLayout(new FlowLayout());  
addWidgets();
//添加背景音乐
AudioClip clip = Applet.newAudioClip(getClass().getResource(music));  
clip.play();
clip.loop();
//向frame中添加panel  
con.getContentPane().add(Pl,BorderLayout.NORTH); //北边
//关闭窗口时退出  
con.getContentPane().add(P2,BorderLayout.SOUTH); //南边
con.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
//显示转换器  
//con.pack(); //限制改变窗体大小
con.setVisible(true); //可见
}  
//为转换器创建和增加widgets  
private void addWidgets_1(){  
//创建widgets.  
txt = new JTextField(20);  
Label1 = new JLabel("摄氏温度",SwingConstants.LEFT);  
niu = new JButton("转换……");  
Label2 = new JLabel("华氏温度",SwingConstants.LEFT);  

//诊听转换器按钮发出的事件  
niu.addActionListener(this);
//向容器中添加widgets  
Pl.add(txt);  
Pl.add(Label1);  
Pl.add(niu);  
Pl.add(Label2);  
Label1.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));  
Label2.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));  
}

private void addWidgets(){
txt2 = new JTextField(20);  
Label3 = new JLabel("华氏温度",SwingConstants.RIGHT);  
niu2 = new JButton("转换……");  
Label4 = new JLabel("摄氏温度",SwingConstants.RIGHT);
niu2.addActionListener(this);
P2.add(txt2);  
P2.add(Label3);  
P2.add(niu2);  
P2.add(Label4);  
Label3.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));  
Label4.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));

}

//改变了这个方法
//实现ActionListener接口  
public void actionPerformed(ActionEvent e) { //成功运行
//将摄氏温度转换为双精度小数,并且转换为华氏温度  
if(e.getSource()==niu)//有多个组件的话用这个方法获取事件源
{
int temp =  
(int) (Double.parseDouble(txt.getText()) * 1.8 + 32);  
setBack(temp,i);
System.out.println(temp);
Label2.setText(temp + " 华氏度");  
}
else if(e.getSource()==niu2)
{  
int temp1 =  
(int)(((Double.parseDouble(txt2.getText()))-32)/1.8);  
System.out.println(temp1);

//setBack(temp1,i);
Label4.setText(temp1 + " 摄氏度");  
}
}  
//改变了这个方法
//显示图片函数  
public int setBack(int temp,int p) {  
((JPanel)con.getContentPane()).setOpaque(false);  
int a=temp;  
double n=(a-32)/1.8;
if(n>=30)
{
// Winter.jpg这个图片的位置要跟当前这个类是同一个包下  
System.out.println("图片4出来了");
if(p==1)
con.getLayeredPane().remove(background);
ImageIcon img = new ImageIcon(getClass().getResource("夏.jpg"));  


 background = new JLabel(img);  
con.getLayeredPane().add(background,  
new Integer(Integer.MIN_VALUE));  
background.setBounds(100, 100, img.getIconWidth(), img.getIconHeight());  
}  
else if(n>=25&&n<30)
{
if(p==1)
con.getLayeredPane().remove(background);
System.out.println("图片1出来了");
ImageIcon img = new ImageIcon(getClass().getResource("春.jpg"));  
 background = new JLabel(img);  
con.getLayeredPane().add(background,  
new Integer(Integer.MIN_VALUE));  
background.setBounds(100, 100, img.getIconWidth(), img.getIconHeight());  
}
else if(n>=10&&n<25)
{  
if(p==1)
con.getLayeredPane().remove(background);
System.out.println("图片2出来了");
ImageIcon img = new ImageIcon(getClass().getResource("秋.jpg"));  
 background = new JLabel(img);  
con.getLayeredPane().add(background,  
new Integer(Integer.MIN_VALUE));  
background.setBounds(100, 100, img.getIconWidth(), img.getIconHeight());  
}
else
{
if(p==1)
con.getLayeredPane().remove(background);
System.out.println("图片3出来了");
ImageIcon img = new ImageIcon(getClass().getResource("冬.jpg"));  
 background = new JLabel(img);  
con.getLayeredPane().add(background,  
new Integer(Integer.MIN_VALUE));  
background.setBounds(100, 100, img.getIconWidth(), img.getIconHeight());
}

p=1;
i=p;
return i;
}

public static void main(String args[]) {  
CelsiusConverter a=new CelsiusConverter();
}
}








[解决办法]
没怎么看程序,教你个非常笨的办法。如果你放的音乐是固定的,那么把播放音乐那段代码单独封装成一个方法比如doMusic(),然后用计时器timer类时间间隔为音乐的长度时间,start那个方法。
[解决办法]
比如音乐时间是10s,音乐播放方法是doMusic()

Java code
javax.swing.Timer t = new javax.swing.Timer(10000, this.doMusic());t.start();
[解决办法]
温度切换图片
第一次近来时 触发音乐事情
以后不触发不就可以了

你现在的情况是每次触发 可不从头再来

热点排行