2010-07-10 104 views
1

我想用J2ME構建一個簡單的基於菜單的GUI。菜單條目當前是從Button類派生的類的對象。有什麼辦法可以:按鈕中的圖像-j2me

  1. 替換按鈕中的文本,並有圖像顯示,而不是圖標?

  2. 使文本和圖像並排顯示在同一菜單欄上。

如果我的問題不清楚,請讓我知道,我會編輯它。

回答

6

您可以通過擴展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(); 
    } 
} 
+0

喜BlaXpirit!謝謝回覆。我看了CustomItem。進一步思考,這裏有一些額外的信息:我已經有一個名爲VoiceButton的類,它擴展了Button並實現了一些更多的功能。所以我想要做的是有一個容器類,一個可以有一個VoiceButton對象,並且還有一個與之關聯的圖像。 CustomItem會訣竅嗎? – Sriram 2010-07-15 10:07:07

+0

我根本不推薦擴展Button類。我會編輯我的答案並添加一些示例。 – 2010-07-15 10:11:39

+0

如果你需要在一個按鈕中輸入文字和圖片,請在這裏寫下,我會盡力去做。但最好自己做! – 2010-07-15 11:07:20