Java Swing을 사용하면 모양과 느낌(L&F)을 변경하여 GUI를 사용자 정의할 수 있습니다. . 모양은 구성 요소의 일반적인 모양을 정의하고 느낌은 구성 요소의 동작을 정의합니다. L&F는 LookAndFeel 의 하위 클래스입니다. 클래스 및 각 L&F는 정규화된 클래스 이름으로 식별됩니다. . 기본적으로 L&F는 Swing L&F(메탈 L&F)로 설정되어 있습니다.
L&F를 프로그래밍 방식으로 설정하려면 setLookAndFeel 메서드를 호출할 수 있습니다. () UIManager 수업. Java Swing 클래스를 인스턴스화하기 전에 setLookAndFeel에 대한 호출을 수행해야 합니다. 그렇지 않으면 기본 Swing L&F가 로드됩니다.
구문
public static void setLookAndFeel(LookAndFeel newLookAndFeel) throws UnsupportedLookAndFeelException
예시
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();
}
} 출력


