Today, swing is used to build a login interface that imitates qq2009 and uses open-source look & feel (substance). Some problems have been encountered during use and some skills have been learned.
Substance (https://substance.dev.java.net) has a lot of appearances to choose from, are very beautiful, but also provides a powerful API for developers to use. Substance already exists
I developed to version 5.2rc. I used version 5.1 at the beginning, but I found some bugs during usage. When I used the watermark function provided by substance, A lot of exceptions occurred. This bug was not found in 5.0. Later, I downloaded the latest version 5.2rc. This problem has been solved in version 5.2. If you need it, click here to download it.
The first question: how to fully use look & feel?
You may ask, what is the complete use of look & feel? Let's take a look at it first;
Check whether look & feel is used in the above image? The answer is correct, but not complete, because his title bar has not changed. The complete use of look & feel is as follows:
Do you think it's quite different? For swing veterans, this is a piece of cake, but it is difficult for beginners, because many of the tutorials they see using look & feel are like
Such:
Uimanager. setlookandfeel (New substanceofficeblue2007lookandfeel ());
Or:
Try {
Uimanager. setlookandfeel ("org. jvnet. substance. Skin. substanceravengraphitelookandfeel ");
} Catch (exception e ){
System. Out. println ("substance Raven graphite failed to initialize ");
}
Or I am too fond of cooking. In short, I don't know how to change the title bar before I carefully learn how to use swing.
To change the title bar, you only need to add one sentence in the main method:
Jframe. setdefalooklookandfeeldecorated (true );
If you want to change the jdialog, add:Jdilog. setdefalooklookandfeeldecorated (true );
The following is the final interface.Officeblue2007lookandfeel in substance:
The second question is, how to add a watermark or background to the application?
There are few official documents, and there are no specific examples to illustrate. Later I downloaded the source code of substance to see how to use it.
The following is a piece of core code written in the main method by referring to the official source code.
Jframe. setdefalooklookandfeeldecorated (true );
Jdialog. setdefalookandfeeldecorated (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 watermark skin
Uimanager. setlookandfeel (New substanceofficeblue2007lookandfeel ());
Substancelookandfeel. setskin (skin); // sets the skin.
} Catch (unsupportedlookandfeelexception ex ){
Logger. getlogger (loginframe. Class. getname (). Log (level. Severe, null, ex );
}
The aboveCodeThe position of a Code cannot be changed, that is, substancelookandfeel. setskin (skin );.
It must be in the following sentence of uimanager. setlookandfeel (New substanceofficeblue2007lookandfeel.
Otherwise, you cannot see the watermark effect. There is a comparison between a watermark and a watermark without a watermark (there is a watermark on the left and no watermark on the right ):
I posted another official one: Let's take a look at the office2007 interface written by swing?