开发者

How to center align a list using LWUIT

i am having problem aligning a list items to center. plz help 开发者_运维百科me. here is the code i have done.

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;

import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import javax.microedition.midlet.MIDlet;


/**
 * @author Administrator
 */
public class main extends MIDlet implements ActionListener
{
  private Image logo;
  private Label logoLabel;
  private List menu;
  private Command select,exit;        

  public void startApp() {
    Display.init(this);
    Form form1 =new Form();
    form1.getStyle().setBgColor(0xe5a812);
    try{
        logo=Image.createImage("/sid_logo.png");
    }
    catch(Exception e){
        System.out.println("::::::"+e);
    }
    logoLabel=new Label(logo);
    select=new Command("Select");
    exit=new Command("Exit");

    //form1.setLayout(new BorderLayout());        
    //form1.addComponent(BorderLayout.NORTH,logoLabel);
    BoxLayout boxlayout=new BoxLayout(BoxLayout.Y_AXIS);
    form1.setLayout(boxlayout);
    form1.addComponent(logoLabel);
    logoLabel.setAlignment(Component.CENTER);
    logoLabel.getStyle().setBgColor(0xe5a812);
    menu=new List();
    menu.addItem("LIST 1");
    menu.addItem("LIST 2");
    form1.addComponent(menu);
    form1.addCommand(select);
    form1.addCommand(exit);
    form1.setCommandListener(this);
    form1.show();

  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }
  public void actionPerformed(ActionEvent event){
    //int index=menu.getSelectedIndex();
    if(event.getCommand()==select && menu.getSelectedIndex()==0){
        System.out.println("Check list1");


    }
    if(event.getCommand()==select && menu.getSelectedIndex()==1){
        System.out.println("Check list 2");
    }
    if(event.getCommand()==exit){
        notifyDestroyed();
    }
  }

}


Use list renderer. Extend the class into Label and implement the ListCellRenderer interface. And set the Label alignment to CENTER.

FYI: Use ResourceEditor and develop the own theme.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜