LWUIT_1_2_1学习文档中文版(2)

  • 格式:doc
  • 大小:714.00 KB
  • 文档页数:11

LWUIT_1_2_4学习文档中文版(2)胡鹰第五章使用Layout Managers5.1 BorderLayout这种布局是把屏幕分成了5个区域:东、西、南、北、中;当向一个容器中添加组件的时候,使用addComponent()方法,这个组件的位置就是这个方法里面的第一个参数。

不能有其他的组件位于同一个位置区域里面。

addComponent(BorderLayout.CENTER, component) // preferredoraddComponent(“Center”, co mponent) // valid but error prone中间部分是有尽量大的空间的,其他部分的空间会自动的适应添加在其中的控件的大小。

一般情况下一个容器只用一到两个部分,比如中间部分或是中间部分加上下面部分。

5.2 BoxLayout这种布局方式是把放在容器里面的控件按照要么是横着放或是竖着放,而且都只有一行或是一列。

BoxLayout boxLayout = new BoxLayout(BoxLayout.X_AXIS);横着放到时候,控件的高度是决定于容器的高度的;竖着放时候,控件的宽度是决定于容器的宽度的。

BoxLayout boxLayout = new BoxLayout(BoxLayout.Y_AXIS);5.3 FlowLayout这是容器的默认布局方式。

这种方式下,横着放,控件的大小按照它们原来的大小;如果一行放不下了,就起第二行放。

而且还可以控制他们的位置为left、right、center。

FlowLayout exampleLayout = new FlowLayout();...container.setLayout(exampleLayout);container.addComponent(new Button("Button 1"));container.addComponent(new Button("Button 2"));container.addComponent(new Button("Button 3"));container.addComponent(new Button("Button 4"));如果改第一个语句:FlowLayout exampleLayout = new FlowLayout(Component.RIGHT);则:5.4 GridLayout这个布局方式也是十分常见的和常用的,特别是我们在做九宫格的时候,就要用到这样的布局方式。

GridLayout exampleLayout = new GridLayout(0,2);...container.setLayout(exampleLayout);container.addComponent(new Button("Button 1"));container.addComponent(new Button("Button 2"));container.addComponent(new Button("Button 3"));container.addComponent(new Button("Button 4"));5.5 GroupLayout这种布局方式是为了开发GUI的开发者设计的,我们一般很少用到,这里不做叙述。

第六章使用PainterPainter是一个可以在组件的背景上面进行绘画的接口。

Painter自己先把自己画出来,之后这个组件再把自己画出来,之后再把二者绑定在一起。

使用Painter的最大好处就是你可以用代码来控制组件的背景。

比如说使用漂亮的图片来做背景。

现在,假如你要在一个组件背景上画一条线,你可以重复使用这个Painter. Painter diagonalPainter = new Painter() {public void paint(Graphics g, Rectangle rect) {g.drawLine(rect.getX(),rect.getY(),rect.getX() + rect.getSize().getWidth(),rect.getY() + rect.getSize().getHeight());}};//设置一个组件的背景上画笔myComponent.getStyle().setBgPainter(diagonalPainter);//下面是设置一些组件Label myLabel = new Label(Image.createImage("/images/duke.png"));myLabel.setAlignment(Component.CENTER);myLabel.getStyle().setBgTransparency(100);myLabel.getStyle().setBgPainter(diagonalPainter);....Button myButton = new Button("Image and Text Button");myButton.setIcon(Image.createImage("/images/duke.png"));myButton.setAlignment(Component.CENTER);myButton.getStyle().setBgTransparency(100);myButton.getStyle().setBgPainter(diagonalPainter);....RadioButton myRadioButton = new RadioButton("RadioButton");myRadioButton.getStyle().setBgTransparency(100);myRadioButton.getStyle().setBgPainter(diagonalPainter);得到的图形是这样的:第七章使用Style ObjectStyle Object可以设置colors,fonts,transparency,margin,padding,images,borders等属性。

每一个组件都包含有两个方法来在运行时改变该组件的风格:component.getSelectedStyle()和component.getUnselectedStyle()。

风格也可以应用于主题之中,当主题改变的时候,风格也相应的随着改变。

7.1 Color每一个组件都有两个可以调节的color:Foreground color和Background color。

颜色的调节值是以RGB来代表的,这时候是没有透明度通道的,或者说这时候的透明度要另外设定的。

颜色的可用值范围是0x000000到0xffffff(黑到白),也可以用十进制的数字表示(0~255)。

7.2 FontFont要使用Font Object来设定。

LWUIT可以使用Bitmap字体也可以使用本地系统的字体。

7.3 透明度(Transparency)LWUIT支持背景的透明度设置。

使用setBgTransparency()函数来设置即可。

参数的范围为0~255.7.4 Margin and Padding什么意思呢?就是说可以设定一个组件中的边框和它的相邻的组件之间的距离或是这个组件的边框和它内部的其他控件之间的边框的距离。

这是通过一个类似于CSS的盒子模型来实现的。

这于网页设计中的这两个属性是一样的道理。

请参看下面的图形:相关的设置语句如下:// Setting padding with positive valuessetPadding(int top, int bottom, int left, int right)// orientation can be Component.TOP, BOTTOM, LEFT or RIGHTsetPadding(int orientation, int gap)// Setting margin with positive valuessetMargin(int top, int bottom, int left, int right)// orientation can be Component.TOP, BOTTOM, LEFT or RIGHTsetMargin(int orientation, int gap)说明:orientation代表的是设定的只是其中的一个方向的属性,而其他方向的属性是默认的值7.5 Images在Style里面,Images代表的是与组件相关的背景图片。

默认的组件是没有背景图片的,更多资料请参看上一个章节。

7.6 边线(Borders)Style支持设定用户自己的风格的边线。

Borders可以是背景画笔画出来的,也可以是图片边线。

边线可以通过相应的方法进行设定,具体请参看应用文档。

7.7 Style Listener它可以使你监听到风格发生的变化。

比如你想监听背景颜色发生的变化,同时就可以用程序去动态的改变这些风格。

下面的代码展示了如何添加风格监听器和做出相应的改变:myComponent.getStyle().addStyleListener(new StyleListener() {public void styleChanged(String propertyName, Style source) {if (propertyName.equals(Style.FONT)) {System.out.println("Font of myComponent got changed.");}}});7.8 PaintersPainter是用来画组件的背景的。

Painter先自己把自己画出来,然后,组件再把自己显示在它的上方,两者进行重叠显示。

使用setBgPainter()方法可以设定画笔。

看下面的代码:mycomponent.getStyle().setBgPainter(myPainter);第八章主题(Theming)8.1 基本主题Lwuit支持类似于CSS的主题,同时,这种主题比Swing中的Look And Feel要简单。

每一个主题都有自己的风格,这样主题可以被人为的多样化,也可以被用户自己进行设定。

比如要使所有的按钮的背景都变成红色,那么你可以使用下面的这条语句:Button.bgColor=ff0000一个主题可以被打包进一个资源文件(下一章会介绍),而且这个资源文件可以在程序运行的时候被加载到程序中。