)”声音文件:
UIManager.put("OptionPane.questionSound", "sounds/OptionPaneError.wav");
这就是在 Swing 程序中播放与预先定义好的操作关联的听觉提示所涉及的所有内容。
完整示例
为了展示我们刚才描述过的功能,清单 1 中的程序将显示三个单选按钮,让您选择希望使用三种提示设置中的哪一种。您可以参看图 1 中简单的用户界面。
图 1. 示例应用程序界面
程序还显示了两个显示弹出窗口的按钮。启用后,显示弹出窗口的操作将触发声音提示。请您自己将确认对话框出现时播放“ 错误”声音文件的代码行标记出来。
清单 1. 声音示例
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Audio extends JFrame {
public Audio() {
super("Auditory Popups");
setDefaultCloseOperation(EXIT_ON_CLOSE);
UIManager.put("AuditoryCues.playList",
UIManager.get("AuditoryCues.defaultCueList"));
UIManager.put("OptionPane.questionSound",
"sounds/OptionPaneError.wav");
JPanel contentPane = (JPanel)this.getContentPane();
JPanel center = new JPanel();
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton defaultAudio = new JRadioButton("Default", true);
center.add(defaultAudio);
buttonGroup.add(defaultAudio);
defaultAudio.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
UIManager.put("AuditoryCues.playList",
UIManager.get("AuditoryCues.defaultCueList"));
}
});
JRadioButton offAudio = new JRadioButton("Off", false);
center.add(offAudio);
buttonGroup.add(offAudio);
offAudio.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
UIManager.put("AuditoryCues.playList",
UIManager.get("AuditoryCues.noAuditoryCues"));
}
});
JRadioButton onAudio = new JRadioButton("On", false);
center.add(onAudio);
buttonGroup.add(onAudio);
onAudio.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
UIManager.put("AuditoryCues.playList",
UIManager.get("AuditoryCues.allAuditoryCues"));
}
});
contentPane.add(center, BorderLayout.CENTER);
JButton confirmButton = new JButton("Confirmation Dialog");
contentPane.add(confirmButton, BorderLayout.SOUTH);
confirmButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(Audio.this,
"Confirm?");
if (result == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(Audio.this, "Confirmed");
} else {
JOptionPane.showMessageDialog(Audio.this, "Rejected");
}
}
});
JButton messageButton = new JButton("Message Dialog");
contentPane.add(m
|