자바 스윙(Java Swing)은 룩 앤 필(Look and Feel, L&F)을 변경함으로써 GUI를 자유롭게 사용자 정의할 수 있도록 지원합니다. 여기서 '룩(look)'은 컴포넌트의 전반적인 외형을 의미하고, '필(feel)'은 컴포넌트가 실제로 동작하는 방식을 의미합니다.
모든 L&F는 LookAndFeel 클래스의 하위 클래스이며, 각 L&F는 완전한 클래스 이름(Fully Qualified Class Name)으로 식별됩니다. 별도로 지정하지 않으면 기본적으로 스윙 고유의 L&F인 메탈(Metal) L&F가 적용됩니다.
프로그램 코드로 L&F 설정하기
L&F를 코드로 설정하려면 UIManager 클래스의 setLookAndFeel() 메서드를 호출하면 됩니다. 단, 이 호출은 반드시 어떤 스윙 클래스도 인스턴스화하기 이전에 수행해야 합니다. 그렇지 않으면 기본 스윙 L&F가 먼저 로드됩니다.
또한 실행 중에 L&F를 변경한 경우에는 SwingUtilities.updateComponentTreeUI() 메서드를 호출해야 이미 생성된 컴포넌트들에 새로운 L&F가 즉시 반영됩니다.
문법(Syntax)
public static void setLookAndFeel(LookAndFeel newLookAndFeel) throws UnsupportedLookAndFeelException
예제(Example)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LookAndFeelTest extends JFrame implements ActionListener {
private JRadioButton windows, metal, motif;
private ButtonGroup bg;
public LookAndFeelTest() {
setTitle("Look And Feels");
windows = new JRadioButton("Windows");
windows.addActionListener(this);
metal = new JRadioButton("Metal");
metal.addActionListener(this);
motif = new JRadioButton("Motif");
motif.addActionListener(this);
bg = new ButtonGroup();
bg.add(windows);
bg.add(metal);
bg.add(motif);
setLayout(new FlowLayout());
add(windows);
add(metal);
add(motif);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae) {
String LAF;
if(ae.getSource() == windows)
LAF = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
else if(ae.getSource() == metal)
LAF = "javax.swing.plaf.metal.MetalLookAndFeel";
else
LAF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
try {
UIManager.setLookAndFeel(LAF);
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
System.out.println("Error setting the LAF..." + e);
}
}
public static void main(String args[]) {
new LookAndFeelTest();
}
}실행 결과(Output)
프로그램을 실행한 후 라디오 버튼 중 하나를 클릭하면, 선택된 L&F에 맞춰 전체 UI가 즉시 갱신됩니다. 아래는 각각 Windows, Metal, Motif L&F가 적용된 화면입니다.


