Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Look&Feel in Swing

Shulou Source: shulou.com Published: 2022-06-02 05:10:31 10月01日 Update

Editor to share with you how to use Look&Feel in Swing, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Today, I used Swing to do a login interface that imitates QQ2009, using the open source Look&Feel (Substance). I encountered some problems and learned some skills in the process of using it. Substance (https://substance.dev.java.net) has a lot of looks to choose from, and they are all beautiful, and it also provides a powerful API for developers to use. Substance has now developed to the 5.2RC version. At first, I used version 5.1, but I found some bug in the process of using it. When I used the watermarking feature provided in Substance, there were a lot of exceptions. This bug was not available in 5.0. later, I downloaded the 5.2RC version of *, which has been solved in version 5.2.

* question: how to use Look&Feel completely?

This is a piece of cake for Swing veterans, but it is difficult for Swing beginners, because many of the tutorials they see using look&feel are like this:

UIManager.setLookAndFeel (new SubstanceOfficeBlue2007LookAndFeel ())

Or this:

Try {UIManager.setLookAndFeel ("org.jvnet.substance.skin.SubstanceRavenGraphiteLookAndFeel");} catch (Exception e) {System.out.println ("SubstanceRavenGraphite failed to initialize");}

Or I'm too lame. In short, I don't know how to change the title bar until I seriously learn the look and feel of Swing.

It only takes one sentence to change the title bar, which is to add this sentence to the main method:

JFrame.setDefaultLookAndFeelDecorated (true)

The second question is, how to add a watermark or background to an application?

The official documentation says very little, and there are no specific examples of how to use it. Later, I downloaded the source code of Substance to find out how to use it.

The following is a core code I wrote in the main method with reference to the official source code

JFrame.setDefaultLookAndFeelDecorated (true)

JDialog.setDefaultLookAndFeelDecorated (true)

Try {

SubstanceImageWatermark watermark = new SubstanceImageWatermark

(LoginFrame. Class .getResourceAsStream ("/ demo/001.jpg"))

Watermark.setKind (ImageWatermarkKind.SCREEN_CENTER_SCALE)

SubstanceSkin skin = new OfficeBlue2007Skin () .withWatermark (watermark)

/ / initialize the skin with watermark

UIManager.setLookAndFeel (new SubstanceOfficeBlue2007LookAndFeel ())

SubstanceLookAndFeel.setSkin (skin); / / set skin

}

Catch (UnsupportedLookAndFeelException ex) {

Logger.getLogger (LoginFrame. Class. GetName () .log (Level.SEVERE, null, ex)

}

The above is all the contents of the article "how to use Look&Feel in Swing". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Problem watermark article content appearance official that is method title source code skin say process reference learning powerful beautiful difficult in a word not really Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn Apple Linux Redmi