如果用户输入的密码超过5次,我会发出安全警告,然后会弹出一个要求用户解锁的框架,框架也会继续循环播放警告声。用户解锁后,它将跳回登录框。我的问题是在我解锁后,系统跳回到登录框架,但声音仍在运行。为什么呢?
这是我的按钮代码跳回登录框架
Staff s = new Staff();
String id = m_id.getText();
String pass = m_pass.getText();
String position = "Manager";
try{
String sql = "Select * from Staff Where Position='"+position+"'";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
while(rs.next()){
String add = rs.getString("Staff_ID");
s.setStaff_ID(add);
String add2 = rs.getString("Password");
s.setPassword(add2);
}
if((s.getStaff_ID().equals(id)) && (s.getPassword().equals(pass))){
warning(clip);
dispose();
new login().setVisible(true);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null,e.getMessage());
}
我用它来循环声音
private void warning(){
try {
URL url = this.getClass().getClassLoader().getResource("sound/aleart.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.loop(Clip.LOOP_CONTINUOUSLY);
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
我的GUI名称是WarningLogin,我在这里调用warning()
。
public WarningLogin() {
initComponents();
warning();
}
答案 0 :(得分:0)
首先clip
需要是您班级的实例字段...
public class ... {
private Clip clip;
然后当你创建它时,你需要引用这个值......
//Clip clip = AudioSystem.getClip();
clip = AudioSystem.getClip();
现在,我可能会使用WindowListener
,当windowClosing
事件被提出时,请尝试拨打clip.stop()
,因为我很迂腐,clip.close()