-1

我正在使用 lwuit 创建一个包含一些值和一个行侦听器的表。在 j2me 中。我正在尝试向它添加一个按钮和一个侦听器,以便我在表中有一个值,我可以通过它实现一个行侦听器。我使用了以下链接: http: //lwuit.blogspot.in/2010/06/headon-that-table.html。但是当我将按钮添加到表中时,它会出现在表的末尾,因为表模型只接受对象作为添加到表中的输入。

通过使用此链接,我通过以下命令按按钮添加了按钮:

container.addComponent(new Button("Details"));

我还尝试创建网格布局或表格布局并添加带有数字行*列的按钮,但最后一个按钮的侦听器仍然有效,其余按钮无效。关于如何实现这个逻辑的任何想法。我的实际任务是向表中添加一个行点击侦听器。任何概念或编码帮助表示赞赏。

4

1 回答 1

0

同时,我得到了与您分享的朋友的答案。他说我要在网格或表格布局中添加按钮,并且按钮应该像二维数组一样添加,以便正确管理它们的听众。

public class Midlet extends MIDlet implements ActionListener{

Form f;
Container c;
private int ROWS=100;
Button b[][];
private int COLUMNS=3;
public void startApp() {
    Display.init(this);

    f=new Form("grid with buttons");
    c=new Container(new TableLayout(ROWS, COLUMNS));
    b=new Button[ROWS][COLUMNS];
    c.setScrollableX(true);
    c.setScrollableY(true);
    //c.setDraggable(true);
    addElements();


    //f.setScrollable(false);
    f.setScrollVisible(true);
    f.addComponent(c);
    f.show();

}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void addElements()
{
    for(int i=0;i<ROWS;i++)
    {
        for(int j=0;j<COLUMNS;j++)
        {
            b[i][j]=new Button(i+" sdkljf "+j);
            c.addComponent(b[i][j]);
            b[i][j].addActionListener(this);
        }
    }

}


/**
 * 
 * @param message message to be displayed
 * @param title title of the alert
 */
 public void showMsg(String message, String title)
{
    final Dialog d=new Dialog(title);
    d.setLayout(new GridLayout(1, 1));
    Button b=new Button("Ok");
    TextArea msg=new TextArea(message);
    msg.setUIID(message);
    msg.setEditable(false);

    //dialogContainer.addComponent(msg);
    //dialogContainer.addComponent(b);

    d.addComponent(msg);
    d.addComponent(b);

    b.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
             d.dispose();
        }
    });

    d.show();
}

public void actionPerformed(ActionEvent evt) {

    for(int i=0;i<ROWS;i++)
    {
        for(int j=0;j<COLUMNS;j++)
        {
            if(b[i][j]==evt.getComponent())
            {
                showMsg(i+","+j, "sl;dkf;");
            }
        }
    }

}

}
于 2013-10-28T10:49:24.077 回答