Component of LWUIT custom flexible Style

Source: Internet
Author: User

When you use the components of LWUIT, if the layout of a page is complex, there are many components, and there are many pages, commonly used components such as Label, Button, TextField, textArea and others will be very common. When using these components, we often set their Style, which is similar to css in the web. It allows us to customize Border, Font, FgColor, BgColor, Margin, Padding, the code for setting the Style of a component is simple:

Code This. getStyle (). setBorder (Border border)

 

However, most components have styles and selectedstyles. The Button that can be clicked and its subclass also have pressedStyle. the preceding sentence is used as an example. It can only set Border when this component is not selected, when elected, it will return to the system code settings. A page has many components. Most components need to set styles (selected and unselected styles). Although the code is very simple, write down a page, you will find that at least half of your code is spent on layout and setting styles, and the code looks very swollen.

Fortunately, LWUIT is open-source. We can modify its source code to customize these UI methods and find Component. java file, we only need to add several methods in this file to simplify our Style settings.

The following are some methods I have added in the Component. java class,The code I wrote is rough. You can write it in your own way,In theory, each method should have two parameters, which are not selected or selected. When passing parameters, the parameter can be null. Some judgment is required to suit most cases.

 

Code // The code is added starting from row 3.
/**
* Set Custom Font
* @ Param font
*/
Public void setCustomFont (Font font ){
This. getStyle (). setFont (font );
This. getSelectedStyle (). setFont (font );
}
/**
* Set the horizontal direction to Margin.
* @ Param margin
*/
Public void setCustomHorizontalMargin (int margin ){
This. getStyle (). setMargin (Component. LEFT, margin );
This. getStyle (). setMargin (Component. RIGHT, margin );
This. getSelectedStyle (). setMargin (Component. LEFT, margin );
This. getSelectedStyle (). setMargin (Component. RIGHT, margin );
}
/**
* Set custom Border
* @ Param border
*/
Public void setCustomBorder (Border border ){
This. getStyle (). setBorder (border );
This. getSelectedStyle (). setBorder (border );
}
/**
* Set custom FgColor
* @ Param unsectedColor
* Color when not selected
* @ Param selectedColor
* Selected color
*/
Public void setCustomFgColor (int unsectedColor, int selectedColor ){
This. getStyle (). setFgColor (unsectedColor );
This. getSelectedStyle (). setFgColor (selectedColor );
}
/**
* Set a custom Style
* The Style contains the selected and unselected conditions. The attributes include Margin, Padding, Border, FgColor, BgColor, and Font.
* @ Param unselectedStyle
* @ Param selectedStyle
*/
Public void setCustomStyle (Style unselectedStyle, Style selectedStyle ){
This. setStyle (unselectedStyle );
This. setSelectedStyle (selectedStyle );
}

 

The Button class and its subclass are special. It has a pressedStyle, and we need to rewrite some methods.

 

Code // The code is added starting from row 3.
/**
* Set Custom Font
* @ Param font
*/
Public void setCustomFont (Font font ){
Super. setCustomFont (font );
This. getPressedStyle (). setFont (font );
}
/**
* Set custom Border
* @ Param border
*/
Public void setCustomBorder (Border border ){
Super. setCustomBorder (border );
This. getPressedStyle (). setBorder (border );
}
/**
* Set custom FgColor
* @ Param unsectedColor
* FgColor when not selected
* @ Param selectedColor
* FgColor
* @ Param pressedColor
* FgColor when you click
*/
Public void setCustomFgColor (int unsectedColor, int selectedColor, int pressedColor ){
Super. setCustomFgColor (unsectedColor, selectedColor );
This. getPressedStyle (). setFgColor (pressedColor );
}
/**
* Set a custom Style
* @ Param unselectedStyle
* Style when not selected
* @ Param selectedStyle
* Selected Style
* @ Param pressedStyle
* Click the Style
*/
Public void setCustomStyle (Style unselectedStyle, Style selectedStyle, Style pressedStyle ){
Super. setCustomStyle (unselectedStyle, selectedStyle );
This. setPressedStyle (pressedStyle );
}

 

After modifying these basic component classes, we can use these components flexibly. Taking a Button as an example, many buttons are used in an application, such as borders, borders, backgrounds, and underscores (like hyperlinks. We can completely classify these styles into a class, so we will write a class CustomButton inherited from the Button.

Code Import com. sun. lwuit. Button;
Import com. sun. lwuit. Image;
/**
*
* @ Author Sunny Peng
*/
Public class CustomButton extends Button {
/**
* Constructor
*/
Public CustomButton (){

}
/**
* Constructor
* @ Param text
* Input text
*/
Public CustomButton (String text ){

}
/**
* Constructor
* @ Param text
* Text
* @ Param icon
* Image
*/
Public CustomButton (String text, Image icon ){

}
/**
* Constructor
* @ Param text
* Text
* @ Param icon
* Image
* @ Param direction
* Direction: the position of the image and text. The image is located at the bottom of the text, and the image is located at the right of the text.
*/
Public CustomButton (String text, Image icon, int direction ){

}
/**
* Borderless button
*/
Public void setNoBorder (){

}
/**
* No background button
*/
Public void setNoBg (){

}
/**
* No border, no background button
*/
Public void setNoBorderBg (){

}
/**
* Hyperlinks
*/
Public void setURLStyle (){

}
}

You can write and define the method parameters.

The source code I am using is the latest version before version 1.3 (version 1.3 is not available yet). I decompile LWUIT. after jar, the obfuscation errors in the Code are modified and used properly. The following is the source code of LWUIT:

Http://download.csdn.net/source/1856358

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.