Merlin's magic: sound in swing

zhaozj2021-02-16  61

Sender: TTLINK (Skywalker), the letter area: Java Title: Merlin's magic: Swing Sound Send Station: Sun and Moonlight (December 20, 2002 10:22 Friday), station letters

Http://www.cn-java.com/target/news.php?news_id=2137

http://www-900.ibm.com/developerWorks/cn/java/J-MER0730/index.shtml

The Swing architecture has been providing developers to create a method of creating a Java application user interface similar to a local UI. The latest version of Java 2 Platform Standard Edition (Java 2 Platform, Standard Edition) further expands this concept, which provides a mechanism that will feed back the sound and UI operation - the feedback sound mentioned here is customer habitual The beep and jingle sound hearing in the computer speaker. Although this function is closed by default, John Zukowski shows you how to play its strength in this latest article in Magic's magic. An interesting Swing feature has been added in J2SE version 1.4. Swing controls can now provide auditory feedback to respond to specific events, but the function is disabled by default. The new feature helps Swing controls to better imitate the behavior of controls for the local operating system.

Separate basic knowledge SWING uses a pluggable look-and-fel, a PLAF architecture. Developers do not have to hardcode for different components for settings such as color and fonts; inventive, components will request them from the User Interface Manager (User Interface (UI) Manager). As a developer, you can tell the UI Manager to let the user see what kind of interface; you can choose Windows, Motif, or Metal style. The UI Manager can actually tell each component how to display it itself. For components such as icon buttons, the foreground color is controlled by setting the button.Foreground property, as shown below:

UIManager.put ("Button.Foreground", Color.Red);

Here, Button.Foreground is a user interface attribute name, while color.red represents a specific setting. After changing the settings, all new buttons will be red. (There is also a way to change the foreground color of the previously created button.) Although different preset appearances and feelings provide default values ​​for these settings, you can also reset them.

In J2SE 1.4, you can use a similar way to enable auditory feedback. You can understand the UI property name and the appropriate settings. Attributes are named AuditoryCues.PlayList here, setting a String array for the hearing prompt name. The UI Manager will then map these names to the sound file that will play when the specific operation occurs.

The following will give the list of appearances and sounds supported in the feeling of the system. Their names themselves are inexpressed.

CheckBoxMenuItem.commandSoundInternalFrame.closeSoundInternalFrame.maximizeSoundInternalFrame.minimizeSoundInternalFrame.restoreDownSoundInternalFrame.restoreUpSoundMenuItem.commandSoundOptionPane.errorSoundOptionPane.informationSoundOptionPane.questionSoundOptionPane.warningSoundPopupMenu.popupSoundRadioButtonMenuItem.commandSound provided to AuditoryCues.playList property String array of names is just that - a collection of event names. The UI Manager is responsible for mapping these names to a particularly appearance and feeling. You can manually create an array that truly wants to support event names from these names, but this is not required. Fortunately, for common groups, there are already two system defined settings, and one setting can be used for Metal appearance and feel.

These settings provide a lookup key AuditoryCues.AllauditoryCues, which allows you to find an array for all sounds from the UI manager. Once you find this array, you can use the auditorycues.playlist to store it in the UI manager, as shown below:

UIManager.put ("AuditoryCues.PlayList", UIManager.get ("AuditoryCues.Allauditorycues));

You can also use two different lookup keys: auditorycues.noauditorycues, no sound, and auditorycues.defaultcuelist, which only sets sound tips for four optionpane, specifically for Metal appearance and feel.

Once you change the AuditoryCues.PlayList settings, you can use a new set of sound prompts. When a specific operation occurs, the UI manager will check the playlist and find the keys associated with the operation. The UI Manager will follow this key to find the sound file to be loaded and played. If there is no key in the prompt (CUE) array, no sound will be played.

If you don't like a sound, you can replace it by mapping it to another file. For example, in the following code, you will see the "Question" sound being mapped to the "Error" sound file provided by the system:

UIManager.put ("optionpane.questions", "sounds / optionpaneerror.wav");

This is to play all the contents involved in playing the auditory prompt associated with pre-defined operations in a Swing program.

The full example in order to show the functions we have just described, the programs in Listing 1 will display three radio buttons, let you choose which of the three prompt settings. You can see the simple user interface in Figure 1.

Figure 1. Sample application interface

The program also shows two buttons for displaying pop-up windows. Once enabled, the operation of the display pop-up window will trigger a sound prompt. Please play the "Error" sound file marker when you appear the confirmation dialog box.

Listing 1. Sound example 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 (); button button button = 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.default ");}});

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 .pelist ", 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 (messageButton, BorderLayout.NORTH); messageButton.addActionListener (new ActionListener () {public void actionPerformed (ActionEvent e) {JOptionPane.showMessageDialog (Audio.this, " ");}); This.pack (); show (); public static void main (String args []) {new audio ();}}

----- Everyday is a winding road - ftp: //10.11.202.202 ※ Source: · Sun Moonlight BBS.Fudan.edu.cn · [from: the city of vampire]

转载请注明原文地址:https://www.9cbs.com/read-17312.html

New Post(0)