【问题标题】:Image in button - j2me按钮中的图像 - j2me
【发布时间】:2010-07-10 10:48:00
【问题描述】:

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

  1. 替换按钮中的文本并显示图像,类似于图标?

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

如果我的问题不清楚,请告诉我,我会编辑它。

【问题讨论】:

    标签: button java-me icons midp lcdui


    【解决方案1】:

    您可以通过扩展CustomItem class 创建自己的Item,看起来像一个按钮。

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

    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();
        }
    }
    

    【讨论】:

    • 嗨 BlaXpirit!谢谢回复。我查看了 CustomItem。进一步思考,这里有一些额外的信息:我已经有一个名为 VoiceButton 的类,它扩展了 Button 并实现了更多功能。所以我想做的是拥有某种容器类,它可以有一个 VoiceButton 对象以及一个与之关联的图像。 CustomItem 能解决问题吗?
    • 我根本不建议扩展 Button 类。我将编辑我的答案并添加一些示例。
    • 如果您需要一个按钮中的文本和图像,请写在这里,我会尽力做到。但最好自己动手!
    • 我会尝试使用此代码并适合我需要的更多内容(如果尚未这样做)。谢谢!
    • 嘿BlaXpirit!谢谢回复。我设法将文本功能添加到按钮。非常感谢!已接受答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多