当前位置:天才代写 > tutorial > JAVA 教程 > LWUIT自界说机动Style的Component

LWUIT自界说机动Style的Component

2017-11-10 08:00 星期五 所属: JAVA 教程 浏览:345

副标题#e#

当你利用LWUIT的这些Component时,假如一个页面中的机关较量巨大,组件 许多,并且页面较量多时,常用的组件诸如 Label,Button,TextField,TextArea 等会用的很是平凡。用起这些组件时,我们经常会配置它的Style,Style很像 web里的css,它可以或许让我们自界说 Border,Font,FgColor,BgColor,Margin,Padding,配置一个组件的 Style的代码 很简朴:

代码

this.getStyle().setBorder(Border border)

可是大大都的组件都有Style和selectedStyle,可以或许被点击的Button及其子 类尚有pressedStyle,以上面一句为例,它仅仅只能配置这个组件未选中的时候 的Border,当选中它时,又会回到系统代码中设定的容貌。一个页面有许多组件 ,大大都的组件都要配置 Style(选中和未选中的Style),固然代码是很简朴 ,可是一个页面写下来,你会发明你至少一半的代码都花在机关和配置样式上了 ,代码看起来很是臃肿。

亏得LWUIT是开源的,我们可以修改它的源代码来自界说这些UI的要领,找到 Component.java文件,我们只需要在这个文件中添加几个要领来简化我们的 Style配置。

以下是我在Component.java类中添加的一些要领,代码我写的较量粗拙,你 们可以凭据本身的方法来写, 理论上每个要领都应该有两个参数,未选中和选 中的状态,传参时可觉得null,需要举办一些判定以适合大大都的环境。

代码

//1469行起是添加的代码
     /**
      * 配置自界说的Font
      * @param font
      */
     public void setCustomFont(Font font) {
         this.getStyle().setFont(font);
         this.getSelectedStyle().setFont(font);
     }
     /**
      * 配置程度偏向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);
     }
     /**
      * 配置自界说的Border
      * @param border
      */
     public void setCustomBorder(Border border){
         this.getStyle().setBorder(border);
         this.getSelectedStyle().setBorder(border);
     }
     /**
      *配置自界说FgColor
      * @param unsectedColor
      *              未选中时的颜色
      * @param selectedColor
      *              选中时的颜色
      */
     public void setCustomFgColor(int unsectedColor, int  selectedColor){
         this.getStyle().setFgColor(unsectedColor);
         this.getSelectedStyle().setFgColor (selectedColor);
     }
     /**
      * 配置自界说的Style
      *              Style包括选中和未选中的环境 ,属性包括Margin,Padding,Border,FgColor,BgColor,Font等
      * @param unselectedStyle
      * @param selectedStyle
      */
     public void setCustomStyle(Style unselectedStyle, Style  selectedStyle){
         this.setStyle(unselectedStyle);
         this.setSelectedStyle(selectedStyle);
     }


#p#副标题#e#

Button类及其子类就较量非凡,它有一个pressedStyle,我们需要对一些方 法举办重写。

代码

//301行起是添加的代码
     /**
      * 配置自界说的Font
      * @param font
      */
     public void setCustomFont(Font font){
         super.setCustomFont(font);
         this.getPressedStyle().setFont(font);
     }
     /**
      * 配置自界说的Border
      * @param border
      */
     public void setCustomBorder(Border border){
         super.setCustomBorder(border);
         this.getPressedStyle().setBorder(border);
     }
     /**
      * 配置自界说FgColor
      * @param unsectedColor
      *              未选中时的FgColor
      * @param selectedColor
      *              选中时的FgColor
      * @param pressedColor
      *              点击时的FgColor
      */
     public void setCustomFgColor(int unsectedColor, int  selectedColor,int pressedColor){
         super.setCustomFgColor(unsectedColor,  selectedColor);
         this.getPressedStyle().setFgColor (pressedColor);
     }
     /**
      * 配置自界说的Style
      * @param unselectedStyle
      *              未选中时的Style
      * @param selectedStyle
      *              选中时的Style
      * @param pressedStyle
      *              点击时的Style
      */
     public void setCustomStyle(Style unselectedStyle, Style  selectedStyle, Style pressedStyle){
         super.setCustomStyle(unselectedStyle,  selectedStyle);
         this.setPressedStyle(pressedStyle);
     }

#p#分页标题#e#

当修改完这些根基的组件类今后,我们就可以机动的运用这些组件了。以 Button为例,在一个应用措施中会运用到许多Button,有边框的,无边框的,无 配景的,带下划线的(雷同于超链接)等等。我们完全可以把这些样式归到一个 类中,那我们就写一个类CustomButton担任自Button。

代码

import com.sun.lwuit.Button;
import com.sun.lwuit.Image;
/**
  *
  * @author Sunny Peng
  */
public class CustomButton extends Button{
     /**
      * 结构要领
      */
     public CustomButton(){

     }
     /**
      * 结构要领
      * @param text
      *          传入文本
      */
     public CustomButton(String text){

     }
     /**
      * 结构要领
      * @param text
      *          文本
      * @param icon
      *          图片
      */
     public CustomButton(String text,Image icon){

     }
     /**
      * 结构要领
      * @param text
      *          文本
      * @param icon
      *          图片
      * @param direction
      *          偏向,图片和文本的位置,好比图片在文 本下方,图片在文本右边等等。
      */
     public CustomButton(String text,Image icon, int  direction){

     }
     /**
      * 无边框按钮
      */
     public void setNoBorder(){

     }
     /**
      * 无配景按钮
      */
     public void setNoBg(){

     }
     /**
      * 无边框,无配景按钮
      */
     public void setNoBorderBg(){

     }
     /**
      * 超链接形式的按钮
      */
     public void setURLStyle(){

     }
}

以上要领的主体各人可以本身写,要领参数也本身界说。

我此刻用的源代码是1.3版本之前最新的(1.3版本的今朝还不可以或许利用), 是我反编译LWUIT.jar后,修改了代码中夹杂发生的错误,利用正常,下面是 LWUIT源代码的下载地点:

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

 

    关键字:

天才代写-代写联系方式