반응형

Substance look and feel - getting started

This document describes the steps to create a sample Swing application and run it under Substance look and feel.

Since Substance requires JDK 6.0 or higher, if you don't have such an installation on your machine, you need to download it from this site and install it. The command-prompt examples below assume that java executable is in the path. This executable is part of JRE (under bin folder) as well as part of JDK (under bin folder). Consult your OS manual on how to add the relevant folder to the path. Alternatively, you can put the entire path to the java executable in the scripts below.

After you have JDK 6.0+ installed on your machine, create the following Walkthrough.java class:

public class Walkthrough extends JFrame {
  public Walkthrough() {
    super("Sample app");
    this.setLayout(new FlowLayout());
    this.add(new JButton("button"));
    this.add(new JCheckBox("check"));
    this.add(new JLabel("label"));

    this.setSize(new Dimension(25080));
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);

    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        Walkthrough w = new Walkthrough();
        w.setVisible(true);
      }
    });
  }
}

This is a simple frame (that does nothing) with a button, a checkbox and a label. You can create this class in your favourite IDE or in any text editor. Once this class is created, compile it. If you're using an IDE, consult the IDE help on the compilation process. If you're using a simple text editor, you can compile this class by using:

javac Walktrough.java

If you have problems, consult the online help for javac compiler. The compiled Walkthrough.class will be created in the same folder as your Walkthrough.java. In order to run it, use:

java -cp . Walkthrough

You will see the following frame under the default Ocean look and feel:

In order to run the same frame under Substance look and feel, you first need to choose the Substance skin that you would like to use (see the links at the end of this document). Suppose that you choose the Business skin. Now you have the following options:

  • Start your VM with -Dswing.defaultlaf=org.jvnet.substance.skin.SubstanceBusinessLookAndFeel
  • UIManager.setLookAndFeel(new SubstanceBusinessLookAndFeel())
  • UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceBusinessLookAndFeel");

The first option doesn't require any code changes in the application above. Run the following script:

java -Dswing.defaultlaf=org.jvnet.substance.skin.SubstanceBusinessLookAndFeel -cp . Walkthrough

You will see the following exception:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Cannot load org.jvnet.substance.skin.SubstanceBusinessLookAndFeel
	at javax.swing.UIManager.initializeDefaultLAF(UIManager.java:1345)
	at javax.swing.UIManager.initialize(UIManager.java:1432)
	at javax.swing.UIManager.maybeInitialize(UIManager.java:1420)
	at javax.swing.UIManager.getUI(UIManager.java:1007)
	at javax.swing.JPanel.updateUI(JPanel.java:109)
	at javax.swing.JPanel.(JPanel.java:69)
	at javax.swing.JPanel.(JPanel.java:92)
	at javax.swing.JPanel.(JPanel.java:100)
	at javax.swing.JRootPane.createGlassPane(JRootPane.java:527)
	at javax.swing.JRootPane.(JRootPane.java:347)
	at javax.swing.JFrame.createRootPane(JFrame.java:260)
	at javax.swing.JFrame.frameInit(JFrame.java:241)
	at javax.swing.JFrame.(JFrame.java:208)
	at Walkthrough.(Walkthrough.java:10)
	at Walkthrough$1.run(Walkthrough.java:25)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:284)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

This means that the org.jvnet.substance.skin.SubstanceBusinessLookAndFeel class in not found in the classpath. This class is located in the substance.jar that you need to download from the Documents & Files section of the Substance project page. For this example, we assume that the substance.jar is located under C:/temp folder. In order to run the frame under Substance, use the following script:

java -Dswing.defaultlaf=org.jvnet.substance.skin.SubstanceBusinessLookAndFeel -cp .;C:/temp/substance.jar Walkthrough

The result is the same frame under Substance look and feel:

The other two options for setting Substance require changing the code. Go back to your Java editor and replace the main() method by:

  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    try {
      UIManager.setLookAndFeel(new SubstanceRavenGraphiteLookAndFeel());
    catch (Exception e) {
      System.out.println("Substance Raven Graphite failed to initialize");
    }
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        Walkthrough w = new Walkthrough();
        w.setVisible(true);
      }
    });
  }

Note that here we are using another Substance skin, Raven Graphite. In order to compile the new Walkthrough.java, you need to add the substance.jar to the build path. Consult your IDE help if you're using IDE. For command-prompt compilation, use the additional -cp flag:

javac -cp c:/temp/substance.jar Walktrough.java

Now you can run your application without the -Dswing.defaultlaf JVM flag, but you still need to specify the location of the substance.jar as before:

java -cp .;C:/temp/substance.jar Walkthrough

If you don't want to create an explicit dependency on the Substance classes in your code, change your main() method to:

  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    try {
      UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceRavenGraphiteLookAndFeel");
    catch (Exception e) {
      System.out.println("Substance Raven Graphite failed to initialize");
    }
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        Walkthrough w = new Walkthrough();
        w.setVisible(true);
      }
    });
  }

You can run the application the same way as before. Here is how it looks like:

Where to go from here?

  • Read the FAQ
  • Read about the VM flags
  • Read about the client properties
  • Read about the API
  • Read about using skins
  • Download the sources and study the test application in test/Check.java
  • Read the Javadocs of SubstanceLookAndFeel class.

<출처 : https://substance.dev.java.net/docs/getting-started.html >

반응형

우아한 Metal 룩앤필의 Swing이 처음 모습을 드러냈을 때, 미적인 면에서 그 주된 경쟁 상대는 Windows 95 인터페이스였습니다. 약 10년 전의 GUI 수준으로 볼 때 Metal은 당시 일반적인 인터페이스에 비해 매력적이고 우아한 대안이었습니다.

자바 SE 5에서 업데이트된 Ocean 테마는 Metal이 지금까지도 선택 받는 데 기여했으나, 이제는 플랫폼을 넘나드는 Swing의 외관을 전면적으로 개편해야 할 시점입니다.

Nimbus 룩앤필의 세계로 오십시오. Synth에 기반한 새롭고 현대적인 룩앤필의 Nimbus는 애플리케이션에 매우 세련된 느낌을 부여합니다. 또한 Nimbus는 정적 비트맵이 아니라, 온전히 Java 2D 벡터 그래픽을 이용해서 만들어졌기 때문에 크기가 매우 작고(불과 56KB!), 임의의 해상도에서 렌더링이 가능합니다.

사용자 삽입 이미지

그림 3: Metal 테마의 SwingSet3
 
사용자 삽입 이미지


그림 4: Nimbus 테마의 SwingSet3
 

호환성 때문에 Metal은 여전히 Swing의 기본 룩앤필이지만 애플리케이션에서 Nimbus를 사용하도록 업데이트하는 작업은 너무나 간단합니다. 즉, 코드 한 줄로 충분합니다.

UIManager.setLookAndFeel("com.sun.java.swing.plaf..nimbus.NimbusLookAndFeel");

또한 명령줄에 -Dswing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel을 추가하면 Nimbus를 기본 룩앤필로 지정할 수 있습니다. 보다 영구적으로 이 속성을 설정하려면 다음과 같은 코드를

swing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel

<JAVA_HOME>/lib/swing.properties 파일에 추가합니다. swing.properties 파일이 없으면 새로 만들어야 합니다.

Nimbus에 관한 더 자세한 정보는 Nimbus early access 페이지를 참고하십시오.



이 글의 영문 원본은
Nimbus Look and Feel in Java SE 6 Update 10 Beta
에서 보실 수 있습니다.

+ Recent posts