按钮中的图像 - j2me [英] Image in button - j2me

查看:137
本文介绍了按钮中的图像 - j2me的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用J2ME构建一个简单的基于菜单的GUI。菜单条目当前是从Button类派生的类的对象。我有什么方法可以:

I am trying to build a simple menu-based GUI with J2ME. The menu entries are currently objects of classes derived from the class Button. Is there any way I can:


  1. 替换按钮中的文字并改为显示图像,是一种图标?

  1. Replace the text in the button and have an image show instead, sort of an icon?

使文本和图像并排显示在同一菜单栏上。

Make the text and image appear side by side on the same menu bar.

如果我的问题不明确,请告诉我,我会对其进行编辑。

If my question is not clear, please let me know and I will edit it.

推荐答案

你可以通过扩展 Item 看起来像一个按钮ref-impl / midp2.0 / jsr118 / javax / microedition / lcdui / CustomItem.htmlrel =nofollow noreferrertitle =javax.microedition.lcdui.CustomItem> CustomItem

You can create your own Item that looks like a button by extending the CustomItem class.

这是一个工作良好的MIDlet,具有良好的 MyButton 类:

This is a working MIDlet with a good MyButton class:

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.midlet.MIDlet;

public class TestMidlet extends MIDlet implements ItemStateListener {
    class MyButton extends CustomItem {
        private Image _image = null;
        private boolean _down = false;
        private int _clicks = 0;

        public MyButton(Image image) {
            super("");
            _image = image;
        }

        // Button's image
        public void setImage(Image image) {
            _image = image;
            repaint();
        }
        public Image getImage() {
            return _image;
        }

        // Has the button been clicked?
        public boolean isClicked() {
            if(_clicks>0) {
                _clicks -= 1;
                return true;
            }
            return false;
        }

        // Is the button currently down?
        public boolean isDown() {
            return _down;
        }
        public void setDown(boolean down) {
            if(_down)
                _clicks += 1;
            if(down!=_down) {
                _down = down;
                repaint();
                notifyStateChanged();
            }
        }
        public void setDown() {
            setDown(true);
        }
        public void setUp() {
            setDown(false);
        }

        // Minimal button size = image size
        protected int getMinContentHeight() {
            return getImage().getHeight();
        }
        protected int getMinContentWidth() {
            return getImage().getWidth();
        }
        // Preferred button size = image size + borders
        protected int getPrefContentHeight(int width) {
            return getImage().getHeight()+2;
        }
        protected int getPrefContentWidth(int height) {
            return getImage().getWidth()+2;
        }

        // Button painting procedure
        protected void paint(Graphics g, int w, int h) {
            // Fill the button with grey color - background 
            g.setColor(192, 192, 192);
            g.fillRect(0, 0, w, h);
            // Draw the image in the center of the button
            g.drawImage(getImage(), w/2, h/2, Graphics.HCENTER|Graphics.VCENTER);
            // Draw the borders
            g.setColor(isDown()?0x000000:0xffffff);
            g.drawLine(0, 0, w, 0);
            g.drawLine(0, 0, 0, h);
            g.setColor(isDown()?0xffffff:0x000000);
            g.drawLine(0, h-1, w, h-1);
            g.drawLine(w-1, 0, w-1, h);
        }

        // If FIRE key is pressed, the button becomes pressed (down state)
        protected void keyPressed(int c) {
            if(getGameAction(c)==Canvas.FIRE)
                setDown();
        }
        // When FIRE key is released, the button becomes released (up state)
        protected void keyReleased(int c) {
            if(getGameAction(c)==Canvas.FIRE)
                setUp();
        }
        // The same for touchscreens
        protected void pointerPressed(int x, int y) {
            setDown();
        }
        protected void pointerReleased(int x, int y) {
            setUp();
        }
    }

    MyButton button = null;

    public void itemStateChanged(Item item) {
        if(item==button) {
            if(button.isClicked())
                System.out.print("clicked, ");
            System.out.println(button.isDown()?"currently down":"currently up");
        }
    }

    public void startApp() {
        try {
            Form form = new Form("Example");
            button = new MyButton(Image.createImage("/icon.png"));
            form.append(button);
            form.setItemStateListener(this);
            Display.getDisplay(this).setCurrent(form);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
}

这篇关于按钮中的图像 - j2me的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆