大家都知道。设计一个好的软件,无非是要求程序稳定,可扩展,可修改跟可移值。在没有出现j2me之前,写手机程序是一件很痛苦的事情。因为你要针对很多的机型进行设计,因此导致了很少人会去涉及。因为设计这样的程序对程序员来说是个挑战。现在不同了。有了j2me使开放手机软件变的容易,而且很快乐。
定义一个与平台无关的各个参数的类,比如把按键的键值放在这里。这样以后修改起来就很方便。
[code]/**
* Provides platform dependent settings for Sun WTK platform
* 提供初始化获取手机屏幕的高,宽等。
* 注意Nokia平台还是需要调用sizeChange方法重新获取。
* @author Greg Gridin
*/
public class Platform extends Canvas {
static public final byte PLATFORM_BARTEO = 1;
static public final byte PLATFORM_LG = 2;
static public final byte PLATFORM_MOTOROLA = 3;
static public final byte PLATFORM_MOTOROLA_V = 4;
static public final byte PLATFORM_NOKIA = 5;
static public final byte PLATFORM_NOKIA_30 = 6;
static public final byte PLATFORM_SIEMENS = 7;
static public final byte PLATFORM_SAMSUNG = 8;
static public final byte PLATFORM_SONYERICSSON = 9;
static public final byte PLATFORM_SUN_WTK = 10;
static public final byte PLATFORM = PLATFORM_SUN_WTK;
/**
* The width in pixels of a screen
*/
public static int WIDTH;
/**
* The height in pixels of a screen
*/
public static int HEIGHT;
/**
* Indicated whether the phone has hardware support for double buffering for
* screen painting
*/
public static final boolean DOUBLE_BUFFER = true;
/**
* "No-key" key code
*/
public static final int KEY_UNDEFINED = 0;
public static final int KEY_FULL_MODE = 48;
/**
* Key code for the UP game action.
*/
public static final int KEY_UP = -1;
/**
* Key code for the LEFT game action.
*/
public static final int KEY_LEFT = -3;
/**
* Key code for the RIGHT game action.
*/
public static final int KEY_RIGHT = -4;
/**
* Key code for the DOWN game action.
*/
public static final int KEY_DOWN = -2;
/**
* Key code for the FIRE game action.
*/
public static final int KEY_FIRE = -5;
/**
* Key code for left soft key
*/
public static final int KEY_SOFT_LEFT = -6;
/**
* Key code for right soft key
*/
public static final int KEY_SOFT_RIGHT = -7;
/**
* Key code for PHONE_ON key (green button)
*/
public static final int KEY_PHONE_ON = -10;
/**
* Key code for PHONE_OFF key (red button)
*/
public static final int KEY_PHONE_OFF = -11;
/**
* Key code for CLEAR key
*/
public static final int KEY_CLEAR = -8;
/**
* Key code for VOLUME UP key
*/
public static final int KEY_VOLUME_UP = KEY_UNDEFINED;
/**
* Key code for VOLUME DOWN key
*/
public static final int KEY_VOLUME_DOWN = KEY_UNDEFINED;
/**
* Key code for "fictitious" ENTER key Some phones does not have FIRE
* button, we have to find out the replacement
*/
public static final int KEY_ENTER = KEY_FIRE;
public static final int KEY_PAGEUP = 42;
public static final int KEY_PAGEDOWN = 35;
public static final int REFRESH = 53;
protected void paint(Graphics g) {
}
static {
Canvas dummy = new Canvas() {
public void paint(Graphics g) {
}
/** 当整个屏幕大小改变时候,通知其他屏幕,屏幕大小改变了 **/
protected void sizeChanged(int w, int h) {
if(h>HEIGHT){
WIDTH = w;
HEIGHT = h;
}
}
};
dummy. setFullScreenMode(true);
//不知道为什么这里总是不能修正高度
if(dummy.getHeight() > HEIGHT){
WIDTH = dummy.getWidth();
HEIGHT = dummy.getHeight();
}
dummy = null;
//System.out.println("配置正确!");
}
public final static long getTotalMemory(){
long memory = Runtime.getRuntime().totalMemory();
return memory;
}
public final static long getfreeMemory(){
long memory = Runtime.getRuntime().freeMemory();
return memory;
}
} // class [/code]
[code]
/**
* The colors available to midlets.
* 定义一些常用颜色配置
*
*/
public class Color {
/**
* "Transparent" color. This color is valid for Motorola handsets, but may not apply for all phones.
*/
public static final int TRANSPARENT = 0xFFFFFFFF;
/**
* The color white.
*/
public static final int WHITE = 0xFEFEFE;
/**
* The color dark gray.
*/
public static final int DK_GRAY = 0x555555;
/**
* The color black.
*/
public static final int BLACK = 0x000000;
} // class Color
[/code]
[code]
/**
* Sets the style for the application.
* This style is blue/white/black for color screens
*
* @author Greg Gridin
*/
public class Style {
/**
* Plain style small size font
*/
public static final Font PLAIN_SMALL =
Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
/**
* Plain style medium size font
*/
public static final Font PLAIN_MEDIUM =
Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
/**
* Plain style large size font
*/
public static final Font PLAIN_LARGE =
Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE);
/**
* Bold style small size font
*/
public static final Font BOLD_SMALL =
Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
/**
* Bold style medium size font
*/
public static final Font BOLD_MEDIUM =
Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
/**
* Bold style large size font
*/
public static final Font BOLD_LARGE =
Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
/**
* Default font to use for text
*/
public final static Font TEXT_FONT = PLAIN_SMALL;
/**
* Default font to use for header text
*/
public final static Font HEADER_FONT = BOLD_SMALL;
/**
* Default font to use for soft key text
*/
public final static Font SOFTKEY_FONT = BOLD_SMALL;
/**
* Default font to use for text fields
*/
public final static Font TEXT_FIELD_FONT = TEXT_FONT;
/**
* Default color for texts
*/
public static final int TEXT_COLOR = Color.BLACK;
/**
* Color to use for component borders (example: textfield component)
*/
public static final int BORDER_COLOR = Color.BLACK;
/**
* Default color for focused backgrounds
*/
public static final int FOCUSED_BACKGROUND_COLOR = Color.DK_BLUE;
/**
* Default color for focused texts
*/
public static final int FOCUSED_TEXT_COLOR = Color.WHITE;
/**
* Default color for cursor
*/
public static final int CURSOR_TEXT_COLOR = Color.BLACK;
/**
* Default color for cursor background
*/
public static final int CURSOR_BACKGROUND_COLOR = Color.WHITE;
/**
* Default color for scrollbar arrows
*/
public static final int ARROW_COLOR = Color.BLACK;
/**
* Default color for screen title background
*/
public static final int TITLE_BACKGROUND_COLOR = Color.LT_BLUE;
/**
* Default color for screen title text
*/
public static final int TITLE_TEXT_COLOR = Color.BLACK;
/**
* Default color for SoftKeyBar background
*/
public static final int SOFTKEYBAR_BACKGROUND_COLOR = Color.LT_BLUE;
/**
* Default color for SoftKeyBar text
*/
public static final int SOFTKEYBAR_TEXT_COLOR = Color.BLACK;
/**
* Vertical gap, in pixels, between components
*/
public static final int V_GAP = 1;
/**
* Horizontal gap, in pixels, between components
*/
public static final int H_GAP = 1;
/**
* Custom fields horizontal gap, in pixels, between block and separator
*/
public static final int CF_H_GAP = 1;
/**
* Default background color
*/
public static final int BACKGROUND_COLOR = Color.TRANSPARENT;
/**
* Type of text fields
*/
public static final boolean SCROLLABLE_TEXTFIELD = true;
private Style() {
}
} // class Style
[/code]
以上是本人经过互联网学习,上面的一些代码出自一些开源的。
大家可以按照需要修改。共同学习。
定义一个与平台无关的各个参数的类,比如把按键的键值放在这里。这样以后修改起来就很方便。
[code]/**
* Provides platform dependent settings for Sun WTK platform
* 提供初始化获取手机屏幕的高,宽等。
* 注意Nokia平台还是需要调用sizeChange方法重新获取。
* @author Greg Gridin
*/
public class Platform extends Canvas {
static public final byte PLATFORM_BARTEO = 1;
static public final byte PLATFORM_LG = 2;
static public final byte PLATFORM_MOTOROLA = 3;
static public final byte PLATFORM_MOTOROLA_V = 4;
static public final byte PLATFORM_NOKIA = 5;
static public final byte PLATFORM_NOKIA_30 = 6;
static public final byte PLATFORM_SIEMENS = 7;
static public final byte PLATFORM_SAMSUNG = 8;
static public final byte PLATFORM_SONYERICSSON = 9;
static public final byte PLATFORM_SUN_WTK = 10;
static public final byte PLATFORM = PLATFORM_SUN_WTK;
/**
* The width in pixels of a screen
*/
public static int WIDTH;
/**
* The height in pixels of a screen
*/
public static int HEIGHT;
/**
* Indicated whether the phone has hardware support for double buffering for
* screen painting
*/
public static final boolean DOUBLE_BUFFER = true;
/**
* "No-key" key code
*/
public static final int KEY_UNDEFINED = 0;
public static final int KEY_FULL_MODE = 48;
/**
* Key code for the UP game action.
*/
public static final int KEY_UP = -1;
/**
* Key code for the LEFT game action.
*/
public static final int KEY_LEFT = -3;
/**
* Key code for the RIGHT game action.
*/
public static final int KEY_RIGHT = -4;
/**
* Key code for the DOWN game action.
*/
public static final int KEY_DOWN = -2;
/**
* Key code for the FIRE game action.
*/
public static final int KEY_FIRE = -5;
/**
* Key code for left soft key
*/
public static final int KEY_SOFT_LEFT = -6;
/**
* Key code for right soft key
*/
public static final int KEY_SOFT_RIGHT = -7;
/**
* Key code for PHONE_ON key (green button)
*/
public static final int KEY_PHONE_ON = -10;
/**
* Key code for PHONE_OFF key (red button)
*/
public static final int KEY_PHONE_OFF = -11;
/**
* Key code for CLEAR key
*/
public static final int KEY_CLEAR = -8;
/**
* Key code for VOLUME UP key
*/
public static final int KEY_VOLUME_UP = KEY_UNDEFINED;
/**
* Key code for VOLUME DOWN key
*/
public static final int KEY_VOLUME_DOWN = KEY_UNDEFINED;
/**
* Key code for "fictitious" ENTER key Some phones does not have FIRE
* button, we have to find out the replacement
*/
public static final int KEY_ENTER = KEY_FIRE;
public static final int KEY_PAGEUP = 42;
public static final int KEY_PAGEDOWN = 35;
public static final int REFRESH = 53;
protected void paint(Graphics g) {
}
static {
Canvas dummy = new Canvas() {
public void paint(Graphics g) {
}
/** 当整个屏幕大小改变时候,通知其他屏幕,屏幕大小改变了 **/
protected void sizeChanged(int w, int h) {
if(h>HEIGHT){
WIDTH = w;
HEIGHT = h;
}
}
};
dummy. setFullScreenMode(true);
//不知道为什么这里总是不能修正高度
if(dummy.getHeight() > HEIGHT){
WIDTH = dummy.getWidth();
HEIGHT = dummy.getHeight();
}
dummy = null;
//System.out.println("配置正确!");
}
public final static long getTotalMemory(){
long memory = Runtime.getRuntime().totalMemory();
return memory;
}
public final static long getfreeMemory(){
long memory = Runtime.getRuntime().freeMemory();
return memory;
}
} // class [/code]
[code]
/**
* The colors available to midlets.
* 定义一些常用颜色配置
*
*/
public class Color {
/**
* "Transparent" color. This color is valid for Motorola handsets, but may not apply for all phones.
*/
public static final int TRANSPARENT = 0xFFFFFFFF;
/**
* The color white.
*/
public static final int WHITE = 0xFEFEFE;
/**
* The color dark gray.
*/
public static final int DK_GRAY = 0x555555;
/**
* The color black.
*/
public static final int BLACK = 0x000000;
} // class Color
[/code]
[code]
/**
* Sets the style for the application.
* This style is blue/white/black for color screens
*
* @author Greg Gridin
*/
public class Style {
/**
* Plain style small size font
*/
public static final Font PLAIN_SMALL =
Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
/**
* Plain style medium size font
*/
public static final Font PLAIN_MEDIUM =
Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
/**
* Plain style large size font
*/
public static final Font PLAIN_LARGE =
Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE);
/**
* Bold style small size font
*/
public static final Font BOLD_SMALL =
Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
/**
* Bold style medium size font
*/
public static final Font BOLD_MEDIUM =
Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
/**
* Bold style large size font
*/
public static final Font BOLD_LARGE =
Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
/**
* Default font to use for text
*/
public final static Font TEXT_FONT = PLAIN_SMALL;
/**
* Default font to use for header text
*/
public final static Font HEADER_FONT = BOLD_SMALL;
/**
* Default font to use for soft key text
*/
public final static Font SOFTKEY_FONT = BOLD_SMALL;
/**
* Default font to use for text fields
*/
public final static Font TEXT_FIELD_FONT = TEXT_FONT;
/**
* Default color for texts
*/
public static final int TEXT_COLOR = Color.BLACK;
/**
* Color to use for component borders (example: textfield component)
*/
public static final int BORDER_COLOR = Color.BLACK;
/**
* Default color for focused backgrounds
*/
public static final int FOCUSED_BACKGROUND_COLOR = Color.DK_BLUE;
/**
* Default color for focused texts
*/
public static final int FOCUSED_TEXT_COLOR = Color.WHITE;
/**
* Default color for cursor
*/
public static final int CURSOR_TEXT_COLOR = Color.BLACK;
/**
* Default color for cursor background
*/
public static final int CURSOR_BACKGROUND_COLOR = Color.WHITE;
/**
* Default color for scrollbar arrows
*/
public static final int ARROW_COLOR = Color.BLACK;
/**
* Default color for screen title background
*/
public static final int TITLE_BACKGROUND_COLOR = Color.LT_BLUE;
/**
* Default color for screen title text
*/
public static final int TITLE_TEXT_COLOR = Color.BLACK;
/**
* Default color for SoftKeyBar background
*/
public static final int SOFTKEYBAR_BACKGROUND_COLOR = Color.LT_BLUE;
/**
* Default color for SoftKeyBar text
*/
public static final int SOFTKEYBAR_TEXT_COLOR = Color.BLACK;
/**
* Vertical gap, in pixels, between components
*/
public static final int V_GAP = 1;
/**
* Horizontal gap, in pixels, between components
*/
public static final int H_GAP = 1;
/**
* Custom fields horizontal gap, in pixels, between block and separator
*/
public static final int CF_H_GAP = 1;
/**
* Default background color
*/
public static final int BACKGROUND_COLOR = Color.TRANSPARENT;
/**
* Type of text fields
*/
public static final boolean SCROLLABLE_TEXTFIELD = true;
private Style() {
}
} // class Style
[/code]
以上是本人经过互联网学习,上面的一些代码出自一些开源的。
大家可以按照需要修改。共同学习。