j2me Canvas中字符串居中

开始想当然的如此写下Canvas下字符串居中的语句

String s = "hello"; g.drawString(s, getWidth()/2, getHeight()/2, Graphics.HCENTER|Graphics.VCENTER);  

发现不对,有异常

 

应该如是写:

String s = "hello"; int height = Font.getDefaultFont().getHeight(); g.drawString(s, this.getWidth() / 2, this.getHeight() / 2 - height/2, Graphics.HCENTER | Graphics.TOP);

 

你可能感兴趣的:(String,j2me)