在Button LWUIT Java ME中设置图标

时间:2012-06-16 08:56:02

标签: java lwuit-button

有人可以建议如何设置图标按钮吗?这是我的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tajwed;

import javax.microedition.midlet.*;

    import com.sun.lwuit.*;
    import com.sun.lwuit.animations.*;
    import com.sun.lwuit.events.*;
    import com.sun.lwuit.layouts.BoxLayout;
    import com.sun.lwuit.plaf.*;
    import java.io.IOException;
    import java.util.Hashtable;

/**
 * @author Muhamad BUrhanudin
 */
public class tajwedMidlet extends MIDlet implements ActionListener{

    Form    mHomeForm;
    Form    mAwayForm;
    Form    mMenuTajwid;

    Command mExitCommand;

    Button btMenu;
    Button btNunSukun, btMimSukun, btNunTasjid;
    Button btLamtarif, btIdgham, btMaad, btRaa;
    Button btHelp;
    Button btExit;

    Command mBackCommand;


    public void startApp() {
        Display.init(this);

        installTheme();
        createUI();
        mHomeForm.show();

    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {

    }

    public void actionPerformed(ActionEvent ae)
    {
            mAwayForm.setTransitionInAnimator(
                Transition3D.createCube(400, false));

            mMenuTajwid.setTransitionInAnimator(
                Transition3D.createCube(400, false));


            mMenuTajwid.setTransitionOutAnimator(
                Transition3D.createCube(400, true));

            mAwayForm.setTransitionOutAnimator(
                Transition3D.createCube(400, true));

        if ((ae.getSource()==btMenu)|| (ae.getSource()==btHelp))
        {
            //mAwayForm.show();
            if(ae.getSource()== btMenu)
            {
                mMenuTajwid.show();
            }
        }
        else if (ae.getSource() == mBackCommand) {
            mHomeForm.show();
        }
        else if ((ae.getCommand() == mExitCommand) || (ae.getSource()== btExit))

          notifyDestroyed();
    }

    private void installTheme()
    {
       UIManager uim = UIManager.getInstance();
        Hashtable ht = new Hashtable();
        ht.put("sel#" + Style.BG_COLOR, "ffffff");
        ht.put(Style.BG_COLOR, "d5fff9");
        ht.put(Style.FG_COLOR, "000000");
        uim.setThemeProps(ht);

     }
    private void createUI() {
      // Set up screen for transitions.
      mAwayForm = new Form("Away");
      mAwayForm.addComponent(new Label("Choose Back to return to the home screen."));

      mMenuTajwid = new Form("MENU DASAR TAJWID");
    //  mMenuTajwid
      mMenuTajwid.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

      btNunSukun = new Button("Hukum Nun Sukun & Tanwin");
      btNunSukun.addActionListener(this);
      mMenuTajwid.addComponent(btNunSukun);

      btMimSukun = new Button("Hukum Mim Sukun");
      btMimSukun.addActionListener(this);
      mMenuTajwid.addComponent(btMimSukun);

      btNunTasjid = new Button("Hukum Nun Tasydid & Min Tasydid");
      btNunTasjid.addActionListener(this);
      mMenuTajwid.addComponent(btNunTasjid);

      btLamtarif = new Button("Hukum Laam Ta'rief");
      btLamtarif.addActionListener(this);
      mMenuTajwid.addComponent(btLamtarif);

      btIdgham = new Button("Idgham");
      btIdgham.addActionListener(this);
      mMenuTajwid.addComponent(btIdgham);

      btMaad = new Button("Maad");
      btMaad.addActionListener(this);
      mMenuTajwid.addComponent(btMaad);

      btRaa = new Button("Raa'");
      btRaa.addActionListener(this);
      mMenuTajwid.addComponent(btRaa);

      mBackCommand = new Command("Back");
      mMenuTajwid.addCommand(mBackCommand);
      mMenuTajwid.addCommandListener(this); // Use setCommandListener() with LWUIT 1.3 or earlier.

      // Set up main screen.
      mHomeForm = new Form("Java Mobile Learning");
      mHomeForm.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

      btMenu = new Button("TAJWID LEARNING");
      btMenu.addActionListener(this);
      mHomeForm.addComponent(btMenu);

      try
      {
      btHelp = new Button("HELP",Image.createImage("/help.ico"));
      btHelp.addActionListener(this);
      mHomeForm.addComponent(btHelp);
      }
      catch(IOException e)
      {

      }

      btExit = new Button("EXIT");
      btExit.addActionListener(this);
      mHomeForm.addComponent(btExit);

      mExitCommand = new Command("Keluar");
      mHomeForm.addCommand(mExitCommand);
      mHomeForm.addCommandListener(this); // Use setCommandListener() with LWUIT 1.3 or earlier.
    }
}

2 个答案:

答案 0 :(得分:0)

您将额外的Image参数传递给按钮构造函数;使用ImageIO类构建图像。

答案 1 :(得分:0)

我假设你想要一个图像(Icon),当点击它时会执行一个动作。即。你不想要一个厚厚的矩形按钮。

以防万一:

Button myButton = new Button("HELP",Image.createImage("/help.ico"));

    myButton.getStyle().setBgImage(null);
    myButton.getStyle().setPadding(0, 0, 0, 0);
    myButton.getStyle().setBorder(null);

    myButton.getPressedStyle().setBgImage(null);
    myButton.getPressedStyle().setPadding(0, 0, 0, 0);
    myButton.getPressedStyle().setBorder(null);

OR:

更简单但内存密集的方式是:

myButton.setFlatten(true);

OR:

使用资源编辑器...一个GUI,您可以为不同的组件指定样式等,如按钮,标签等。