IBM Korea Skip to main content
       IBM Ȩ    |  Á¦Ç° & ¼­ºñ½º  |  °í°´Áö¿ø & ´Ù¿î·Îµå  |  È¸¿ø°¡ÀÔ  

Magic with Merlin: ¿Àµð¿À ½ºÀ®(Swing)
¸ñ Â÷:
¼Ò¸®(Auditory)ÀÇ ±âÃÊ
¿¹Á¦
Âü°íÀÚ·á
ÇÊÀÚ¼Ò°³
±â»ç¿¡ ´ëÇÑ Æò°¡
°ü·Ã dW ¸µÅ©:
Build a better GUI
Tutorial: JMF basics
Enterprise Media Beans (from alphaWorks)
Subscribe to the developerWorks newsletter
US ¿ø¹® Àбâ
Also in the Java zone:
Tutorials
Tools and products
Code and components
Articles
»ç¿ëÀÚ ÀÎÅÍÆäÀ̽º Çâ»óÀ» À§ÇÑ ¼Ò¸® ½ÅÈ£ ±¸ÇöÇϱâ

John Zukowski
President, JZ Ventures, Inc.
2002³â 7¿ù

Column iconSwing ¾ÆÅ°ÅØÃÄ´Â °³¹ßÀÚµéÀÌ ¿ø½Ã UI¸¦ ¸ð¹æÇÏ´Â ÀÚ¹Ù ¾ÖÇø®ÄÉÀ̼ǿë À¯Àú ÀÎÅÍÆäÀ̽º¸¦ ¸¸µå´Â ¼ö´ÜÀ» Á¦°øÇß´Ù. Java 2 Platform, Standard EditionÀº ÀÌ ¾ÆÀ̵ð¾î¸¦ Àû¿ëÇÏ¿© UI ÀÛµ¿°ú Çǵå¹é »ç¿îµå¸¦ Á¶ÇÕÇÏ´Â ¸ÞÄ¿´ÏÁòÀ¸·Î °³³äÀ» ¹ßÀü½ÃÄ×´Ù.

J2SE version 1.4¿¡´Â »õ·Ó°í Àç¹ÌÀÖ´Â Swing ±â´ÉÀÌ ÀÖ¾î ´«±æÀ» ²ö´Ù. Swing Á¦¾î´Â ƯÁ¤ À̺¥Æ®¿¡ ´ëÇÑ ÀÀ´äÀ¸·Î ¼Ò¸® Çǵå¹éÀ» Á¦°øÇÒ ¼ö ÀÖ´Ù. ÇÏÁö¸¸ ÀÌ ±â´ÉÀº µðÆúÆ® »óÅ¿¡¼­´Â »ç¿ëÇÒ ¼ö ¾ø´Ù. Ãß°¡µÈ ±â´É ´öÅÿ¡ Swing Á¦¾î°¡ ½Ã½ºÅÛÀÇ ¿ø½Ã ¿î¿µ üÁ¦¸¦ Àß ¸ð¹æÇÒ ¼ö ÀÖµµ·Ï ÇÑ´Ù.

¼Ò¸®(Auditory)ÀÇ ±âÃÊ
SwingÀº Pluggable Look-and-Feel (PLAF) ¾ÆÅ°ÅØÃÄ¿Í ÇÔ²² ÀÛµ¿ÇÑ´Ù. ´Ù¾çÇÑ ÄÄÆ÷³ÍÆ®ÀÇ »ö±ò°ú ÆùÆ®¸¦ ¼³Á¤ÇϱâÀ§ÇØ ÄÚµùÀ» ÇÒ ÇÊ¿ä°¡ ¾ø´Ù. ´ë½Å ÄÄÆ÷³ÍÆ®´Â Interface (UI) Manager·Î ºÎÅÍ ÀÌ ¼³Á¤µéÀ» ¿äûÇÑ´Ù. °³¹ßÀÚµéÀº UI Manager¿¡°Ô »ç¿ëÀÚ°¡ º¸±â ¿øÇÏ´Â ÀÎÅÍÆäÀ̽º°¡ ¹«¾ùÀÎÁö¸¦ ¸í·ÉÇÒ ¼ö ÀÖ´Ù; ¿É¼ÇÀ¸·Î´Â Windows, Motif, Metal styles µîÀÌ ÀÖ´Ù. UI Manager´Â °¢ ÄÄÆ÷³ÍÆ®¿¡°Ô À̵éÀ» ¾î¶»°Ô µð½ºÇ÷¹ÀÌ ÇؾßÇÏ´ÂÁö¸¦ ¸í·ÉÇÑ´Ù. ¹öÆ° °°Àº ÄÄÆ÷³ÍÆ®ÀÇ °æ¿ì Àü¸é »ö»óÀº Button.foreground ¼Ó¼ºÀ» ¼³Á¤ÇÏ¿© Á¦¾îµÈ´Ù:

UIManager.put("Button.foreground", Color.red);

Button.foreground´Â »ç¿ëÀÚ ÀÎÅÍÆäÀ̽º ¼Ó¼º À̸§ÀÌ°í Color.red´Â ƯÁ¤ ¼³Á¤ÀÌ´Ù. ¼³Á¤À» º¯°æÇÑ ÈÄ¿¡ ¸ðµç »õ·Î¿î ¹öÆ°Àº ºÓÀº »öÀÌ µÈ´Ù. (ÀÌÀü¿¡ ¸¸µé¾îÁø ¹öÆ°À» º¯°æÇÏ´Â ¹æ¹ýµµ ÀÖ´Ù.)

J2SE 1.4¿¡¼­ ¼Ò¸® Çǵå¹éÀ» ºñ½ÁÇÑ ÇüÅ·Π½ÇÇàÇÒ ¼ö ÀÖ´Ù. UI ¼Ó¼º À̸§°ú ¾Ë¸ÂÀº ¼³Á¤À» ¾Ë°íÀÖÀ¸¸é µÈ´Ù. ¿©±â¿¡¼­ ¼Ó¼ºÀº AuditoryCues.playListÀÌ°í ¼³Á¤Àº ¼Ò¸® ½ÅÈ£¿¡ ´ëÇÑ À̸§µéÀÇ String ¾î·¹ÀÌ ÀÌ´Ù. UI Manager´Â ÀÌ·¯ÇÑ À̸§µéÀ» ƯÁ¤ ¾×¼ÇÀÌ ¹ß»ýÇÒ ¶§ ÀÛµ¿ÇÏ´Â ¼Ò¸® ÆÄÀÏ·Î ¸ÅÇÎÇÑ´Ù.

´ÙÀ½Àº ½Ã½ºÅÛÀÌ Á¦°øÇÏ´Â ·è¾ØÇÊ(look-and-feel)ÀÇ »ç¿îµå ¸®½ºÆ®ÀÌ´Ù. °¢°¢ À̸§µéÀº ÀÚ°¡ ¼³¸íÀû(self-explanatory)ÀÌ´Ù.

  • CheckBoxMenuItem.commandSound
  • InternalFrame.closeSound
  • InternalFrame.maximizeSound
  • InternalFrame.minimizeSound
  • InternalFrame.restoreDownSound
  • InternalFrame.restoreUpSound
  • MenuItem.commandSound
  • OptionPane.errorSound
  • OptionPane.informationSound
  • OptionPane.questionSound
  • OptionPane.warningSound
  • PopupMenu.popupSound
  • RadioButtonMenuItem.commandSound

AuditoryCues.playList ¼Ó¼º¿¡ Á¦°øµÈ À̸§µéÀÇ String ¾î·¹ÀÌ´Â À̺¥Æ® À̸§µéÀÇ ¸ðÀ½ÀÏ »ÓÀÌ´Ù. ÀÌ·¯ÇÑ À̸§µéÀ» ·è¾ØÇÊÀÌ ÁöÁ¤ÇÑ »ç¿îµå·Î ¸ÅÇÎÇÏ´Â °ÍÀº UI Manager°¡ ÇÒ ÀÏÀÌ´Ù.

ÀÌ·¯ÇÑ À̸§µé¿¡¼­ Áö¿øÇϱ⠿øÇÏ´Â ½ÇÁ¦ À̺¥Æ® À̸§ÀÇ ¾î·¹À̸¦ ¼öµ¿À¸·Î Á¶ÀÛÇÒ ¼ö ÀÖÁö¸¸ ºÒÇÊ¿äÇÏ´Ù. ´ÙÇàÈ÷µµ ÀϹÝÀûÀÎ »ç¿ë ±×·ìÀ» À§ÇÑ µÎ °³ÀÇ ½Ã½ºÅÛ Á¤ÀÇ ¼³Á¤ÀÌ ÀÖ´Ù.

¼³Á¤Àº AuditoryCues.allAuditoryCues °Ë»ö Å°(look-up key)¸¦ Á¦°øÇÏ´Â µ¥, À̸¦ »ç¿ëÇÏ¿© UI Manager¿¡¼­ ³ª¿À´Â ¸ðµç »ç¿îµå¿¡ ´ëÇÑ ÀûÀýÇÑ ¾î·¹À̸¦ ãÀ» ¼ö ÀÖ´Ù. ÀÏ´Ü ¾î·¹À̸¦ ãÀ¸¸é UI Manager¿¡ ÀÖ´Â AuditoryCues.playList Å°¸¦ »ç¿ëÇÏ¿© À̸¦ ÀúÀåÇÒ ¼ö ÀÖ´Ù:


  UIManager.put("AuditoryCues.playList",
    UIManager.get("AuditoryCues.allAuditoryCues"));

»ç¿ëÇÒ ¼ö ÀÖ´Â µÎ °³ÀÇ ´Ù¸¥ °Ë»ö Å° °ªÀÌ ÀÖ´Ù: AuditoryCues.noAuditoryCues(¼Ò¸®¾øÀ½)°ú AuditoryCues.defaultCueList´Â ³× °³ÀÇ OptionPane ¼³Á¤À» À§ÇÑ »ç¿îµå ½ÅÈ£¸¦ ÀÛµ¿ÇÏ°í Metal look-and-feel¿¡ ÇÑÁ¤µÇ¾îÀÖ´Ù.

AuditoryCues.playList ¼³Á¤À» º¯°æÇÏ¸é »õ·Î¿î ¿Àµð¿À ½ÅÈ£¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù. Ưº°ÇÑ ÀÛµ¿ÀÌ ¹ß»ýÇϸé UI Manager´Â ±× ÀÛµ¿°ú °ü°èÀÖ´Â Å°ÀÇ Ç÷¹À̸®½ºÆ®¸¦ °Ë»çÇÑ´Ù. UI Manager´Â ·ÎµùÇÏ¿© ½ÇÇàÇÒ »ç¿îµå ÆÄÀÏÀ» ã±âÀ§ÇØ ±× Å°¸¦ »ç¿ëÇÑ´Ù. ½ÅÈ£ ¾î·¹ÀÌ¿¡ Å°°¡ ¾ø´Ù¸é ¾î¶² ¼Ò¸®µµ ½ÇÇàµÇÁö ¾Ê´Â´Ù.

ƯÁ¤ »ç¿îµå¸¦ ¿øÇÏÁö ¾ÊÀ¸¸é ½ÅÈ£ À̸§À» ´Ù¸¥ ÆÄÀÏ·Î ¸ÅÇÎÇÏ¿© ´ëüÇÒ ¼ö ÀÖ´Ù. ¿¹¸¦µé¾î ¾Æ·¡ Äڵ忡¼­ ½Ã½ºÅÛ¿¡¼­ Á¦°øÇÏ´Â ¿¡·¯ »ç¿îµå ÆÄÀÏ·Î ¸ÅÇÎµÈ question sound¸¦ º¸°ÔµÈ´Ù:


  UIManager.put("OptionPane.questionSound", "sounds/OptionPaneError.wav");

ÀÌ°ÍÀÌ Swing ÇÁ·Î±×·¥¿¡¼­ »çÀüÁ¤ÀÇµÈ ¾×¼Ç°ú °ü°è ÀÖ´Â ¼Ò¸® ½ÅÈ£¸¦ ÀÛµ¿ÇÏ´Â ¹æ¹ýÀÌ ÀüºÎÀÌ´Ù.

¿¹Á¦
Listing 1ÀÇ ÇÁ·Î±×·¥Àº ¿øÇÏ´Â ¼¼ °³ÀÇ ½ÅÈ£ ¼³Á¤À» ¼±ÅÃÇÒ ¼ö ÀÖ´Â ¼¼ °³ÀÇ ¶óµð¿À ¹öÆ°À» ³ªÅ¸³½´Ù. ±×¸² 1¿¡¼­ °£´ÜÇÑ »ç¿ëÀÚ ÀÎÅÍÆäÀ̽º¸¦ º¼ ¼ö ÀÖ´Ù.

±×¸² 1. ¿¹Á¦ ¾ÖÇø®ÄÉÀÌ¼Ç ÀÎÅÍÆäÀ̽º
Example application interface

Æ˾÷ À©µµ¿ì¸¦ µð½ºÇ÷¹ÀÌÇÏ´Â µÎ °³ÀÇ ¹öÆ°ÀÌ ÀÖ´Ù. Æ˾÷ÀÌ µð½ºÇ÷¹ÀÌ µÉ ¶§ ¼Ò¸® ½ÅÈ£¸¦ ¹ß»ý½ÃŲ´Ù.

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

Âü°íÀÚ·á

ÇÊÀÚ¼Ò°³
John ZukowskiJohn Zukowski´Â JZ Ventures, Inc. ¿¡¼­ Àü·«Àû ÀÚ¹Ù ÄÁ¼³ÆÃÀ» ¼öÇàÇÏ°í ÀÖÀ¸¸ç jGuru Ä¿¹Â´ÏƼ¿¡¼­ ¿î¿µÇÏ´Â Java FAQsÀÇ ±¸·ç(guru)·Î È°µ¿ÇÏ°í ÀÖ´Ù. Àú¼­·Î´Â Mastering Java 2, J2SE 1.4 ¿Í Learn Java with JBuilder 6 µîÀÌ ÀÖ´Ù.
ÀÌ ±â»ç¿¡ ´ëÇÏ¿© ¾î¶»°Ô »ý°¢ÇϽʴϱî?

Á¤¸» ÁÁ´Ù (5) ÁÁ´Ù (4) ±×Àú±×·¸´Ù (3) ¼öÁ¤º¸¿ÏÀÌ ÇÊ¿äÇÏ´Ù(2) ÇüÆí¾ø´Ù (1)

  È¸»ç¼Ò°³  |  °³ÀÎÁ¤º¸ º¸È£Á¤Ã¥  |  ¹ý·ü  |  ¹®ÀÇ