课外天地 李树青学习天地JavaME移动开发课件 → 程序代码——自定义窗体组件:图像性别单选钮


  共有15052人关注过本帖树形打印复制链接

主题:程序代码——自定义窗体组件:图像性别单选钮

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1940 积分:26616 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——自定义窗体组件:图像性别单选钮  发帖心情 Post By:2008/10/14 21:04:42 [只看该作者]

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet implements CommandListener {
        private Display display;
        Form form = new Form("自定义组件");
        GenderChooser gc = new GenderChooser("选择性别");
        Command cmdOK = new Command("显示", Command.OK, 1);
        TextField tf = new TextField("选择的性别是:", "", 20, TextField.ANY);

        public Exec() {
                display = Display.getDisplay(this);
        }

        public void startApp() {
                form.append(gc);
                form.append(tf);
                form.addCommand(cmdOK);
                form.setCommandListener(this);
                display.setCurrent(form);
        }

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
        }

        public void commandAction(Command arg0, Displayable arg1) {
                tf.setString(gc.getString());
        }
}

class GenderChooser extends CustomItem implements ItemCommandListener {
        Image img1, img2;
        Command selection = new Command("选择", Command.BACK, 1);
        int image1Width, image1Height;
        int image2Width, image2Height;
        int checked = 0;
        int location = 0;

        public GenderChooser(String title) {
                super(title);
                try {
                        img1 = Image.createImage("/images/male.gif");
                        img2 = Image.createImage("/images/female.gif");
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                }
                setDefaultCommand(selection);
                setItemCommandListener(this);
                image1Width = img1.getWidth();
                image1Height = img1.getHeight();
                image2Width = img2.getWidth();
                image2Height = img2.getHeight();
        }

        protected int getMinContentHeight() {
                return (image1Height >= image2Height) ? (2 * image1Height + 10)
                                : (2 * image2Height + 10);
        }

        protected int getMinContentWidth() {
                return (image1Width >= image2Width) ? (2 * image1Width + 10)
                                : (2 * image2Width + 10);
        }

        protected int getPrefContentHeight(int arg0) {
                return getMinContentHeight();
        }

        protected int getPrefContentWidth(int arg0) {
                return getMinContentWidth();
        }

        protected boolean traverse(int dir, int viewportWidth, int viewportHeight,
                        int[] visRect_inout) {
                location = (location == 1) ? 2 : 1;
                repaint();
                return true;
        }

        protected void paint(Graphics g, int arg1, int arg2) {
                int width = getMinContentWidth();
                int height = getMinContentHeight();
                g.setStrokeStyle(Graphics.DOTTED);
                g.drawRect(0, 0, width - 1, height - 1);
                g.setStrokeStyle(Graphics.SOLID);
                g.drawArc(4, 4, 13, 13, 0, 360);
                g.drawArc(4, height / 2 + 5, 13, 13, 0, 360);
                if (checked == 1)
                        g.fillArc(4, 4, 13, 13, 0, 360);
                else if (checked == 2)
                        g.fillArc(4, height / 2 + 5, 13, 13, 0, 360);
                if (location == 1)
                        g.drawRect(2, 2, width - 5, height / 2 - 5);
                else
                        g.drawRect(2, height / 2, width - 5, height / 2 - 5);
                g.drawImage(img1, 22, 1, Graphics.TOP | Graphics.LEFT);
                g.drawImage(img2, 22, height / 2, Graphics.TOP | Graphics.LEFT);
        }

        public void commandAction(Command arg0, Item arg1) {
                checked = location;
                repaint();
        }

        public String getString() {
                if (checked == 1)
                        return "男";
                else
                        return "女";
        }
}

[此贴子已经被作者于2010-12-12 18:37:46编辑过]

 回到顶部