|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
Java编译的是字节码,跟C++相反,启动不够快,效率不够高,难以精确控制内存,但是优点是编程比C++容易,代码比较安全但是容易留下性能隐患,跨平台靠字节码在各个平台复制(一处编译到处调试)编程
/*
*Canvas2let.java
*
*Createdon2005年4月19日,下战书5:27
*/
importjavax.microedition.midlet.*;
importjavax.microedition.lcdui.*;
/**
*
*@authorAdministrator
*@version
*/
publicclassCanvas2letextendsMIDletimplementsCommandListener{
privateChoiceGroupfaceChoice;
privateChoiceGroupstyleChoice;
privateChoiceGroupsizeChoice;
privateCommandokCommand;
privateCommandexitCommand;
privateCommandbackCommand;
privateTextBoxfaceTextBox;
privateTextBoxstyleTextBox;
privateTextBoxsizeTextBox;
privateFormchoiceForm;
privateDisplayaDisplay;
privateMyCanvascanvas;
intface;
intsize;
intstyle;
publicCanvas2let(){
choiceForm=newForm("SelectFont");
faceChoice=newChoiceGroup("表面",Choice.EXCLUSIVE);
faceChoice.append("PROPORTIONAL",null);
faceChoice.append("MONOSPACE",null);
faceChoice.append("SYSTEM",null);
styleChoice=newChoiceGroup("作风",Choice.MULTIPLE);
styleChoice.append("PLAIN",null);
styleChoice.append("BOLD",null);
styleChoice.append("ITALIC",null);
styleChoice.append("UNDERLINED",null);
sizeChoice=newChoiceGroup("巨细",Choice.EXCLUSIVE);
sizeChoice.append("SMALL",null);
sizeChoice.append("MEDIUM",null);
sizeChoice.append("LARGE",null);
exitCommand=newCommand("加入",Command.EXIT,1);
okCommand=newCommand("断定",Command.OK,1);
backCommand=newCommand("前进",Command.BACK,2);
choiceForm.append(faceChoice);
choiceForm.append(styleChoice);
choiceForm.append(sizeChoice);
choiceForm.addCommand(exitCommand);
choiceForm.addCommand(okCommand);
choiceForm.setCommandListener(this);
canvas=newMyCanvas();
}
publicvoidstartApp(){
aDisplay=Display.getDisplay(this);
aDisplay.setCurrent(choiceForm);
}
publicvoidpauseApp(){
}
publicvoiddestroyApp(booleanunconditional){
}
publicvoidcommandAction(Commandc,Displayabled){
if(c==exitCommand){
destroyApp(false);
notifyDestroyed();
}
else{
//selectthefaceandsize
face=faceChoice.getSelectedIndex();
switch(face){
case0:face=Font.FACE_PROPORTIONAL;break;
case1:face=Font.FACE_MONOSPACE;break;
case2:face=Font.FACE_SYSTEM;break;
}
size=sizeChoice.getSelectedIndex();
switch(size){
case0:size=Font.SIZE_SMALL;break;
case1:size=Font.SIZE_MEDIUM;break;
case2:size=Font.SIZE_LARGE;break;
}
//selectthestyle
boolean[]styleSelect=newboolean[4];
for(inti=0;i<4;i++){
if(styleChoice.isSelected(i)&&i==0){
style|=Font.STYLE_PLAIN;
}
elseif(styleChoice.isSelected(i)&&i==1){
style|=Font.STYLE_BOLD;
}
elseif(styleChoice.isSelected(i)&&i==2){
style|=Font.STYLE_ITALIC;
}
elseif(styleChoice.isSelected(i)&&i==3){
style|=Font.STYLE_UNDERLINED;
}
}
//canvas=newMyCanvas();
aDisplay.setCurrent(canvas);
}
}
classMyCanvasextendsCanvasimplementsCommandListener{
publicMyCanvas(){
addCommand(backCommand);
setCommandListener(this);
}
publicvoidpaint(Graphicsg){
g.setColor(0xFFFFFF);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0);
Strings="HelloWorld";
Fontf=Font.getFont(face,style,size);
g.setFont(f);
g.drawString(s,150,250,Graphics.RIGHT|Graphics.BOTTOM);
}
publicvoidcommandAction(Commandc,Displayabled){
if(c==backCommand){
aDisplay.setCurrent(choiceForm);
}
}
}
}
这个程序次要是加深对Canvas的Font的熟悉。
1.MIDlet类的机关函数应当是publicCanvas2let()而不该该是publicvoidCancas2let()。由于加了void就不会前往该类的对象,招致程序运转后没有反应。这是一个教导。
2.在Canvas中,必要利用代码
g.setColor(0xFFFFFF);
g.fillRect(0,0,getWidth(),getHeight());
来扫除上一个界面。然后再利用g.setColor(0);来设置绘画色彩。
3.关于这一点,应当如许了解:
既然利用了初级界面,那末就意味着屏幕上的统统都回你管,你有最年夜的权力。但你也有响应的任务,扫除屏幕的事情天然也要你来做
因为能用到多少功能就用多少,不能用就不用!总的来说:要简单要性能好,可以不用框架。你说java复杂,就是因为你把java(j2ee)与这些框架混在了一起。 |
|