Java - 모양과 느낌

응용 프로그램을 더 예쁘게 만들기 위해 Swing 아키텍처는 각 구성 요소를 JComponent ComponentUI의 두 가지 클래스로 분리하여 응용 프로그램 또는 GUI의 모양과 느낌을 변경하도록 만들어졌습니다. 예를 들어, JList의 각 인스턴스에는 ComponentUI.

수퍼 클래스에서 상속되는 구체적인 ListUI 구현이 있습니다. UIManager.setLookAndFeel(LookAndFeel l ) LookAndFeel의 클래스 이름을 인수로 사용합니다.

변경 후 메서드를 호출하여 구성 요소 트리를 업데이트해야 합니다.  SwingUtilities.updateComponentTreeUI(프레임);

Example

이 예제에서는 Metal, System, Pattern 및 GTK와 같은 유효한 값 중 하나를 포함하는 LOOKANDFEEL 상수를 선언합니다. 이 코드는 Eclipse 및 Netbeans에서 작동합니다.

import javax.swing.*; 
import java.awt.*;

public class LookAndFeelTest extends JFrame{

public LookAndFeelTest(){
//lookandfeel
initLookAndFeel();
//학습이 적용되었는지 확인
JFrame.setDefaultLookAndFeelDecorated(true);

//창 생성 및 구성
JFrame frame = new JFrame("SwingApp");
컴포넌트 내용 = createComponents();
frame.getContentPane().add(내용, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

SwingUtilities.updateComponentTreeUI(프레임);
//창 표시
frame.pack();
frame.setVisible(true);
}

/*
*
* LOOKANDFEEL 상수
*/
final static String LOOKANDFEEL = "Metal";

공용 컴포넌트 createComponents() {
JButton button = new JButton("Button");
JLabel 레이블 = new JLabel(LOOKANDFEEL);

JPanel 패널 = new JPanel(new GridLayout(0, 1));
panel.add(버튼);
panel.add(레이블);
panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));

리턴 패널;
}

private static void initLookAndFeel() {
문자열 lookAndFeel = null;

if(LOOKANDFEEL != null) {
스위치(LOOKANDFEEL){
"메탈" box:lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
부동산;
"시스템":lookAndFeel = UIManager.getSystemLookAndFeelClassName();
부동산;
"패턴":lookAndFeel = "com.sun.java.swing.plaf.pattern.PatternLookAndFeel";
부동산;
상자 "GTK":lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
부동산;
}

try {
UIManager.setLookAndFeel(lookAndFeel);
}
catch (예외 e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
new LookAndFeelTest();
}
}
출력:
모양과 느낌 메탈 GTK LookAndFeel

System

Windows 7의 외관을 복구한 이 예제와 같이 운영 체제의 원래 테마를 유지하려는 경우.

lookAndFeel = UIManager.getSystemLookAndFeelClassName(); 
Out:
룩 앤 필 SystemLookAndFeel

Motif

lookAndFeel="com.sun.java.swing.plaf.motif.MotifLookAndFeel"; 
Out:
모양과 느낌 MotifLookAndFeel

참조:
Oracle 문서: 모양과 느낌을 설정하는 방법.
Developpez.net: Java GUI:Look & 느낌