繁体   English   中英

如何滚动表单以使指定的组件在LWUIT中可见?

[英]How to scroll a form to make specified component visible in LWUIT?

在向表单添加组件时,我希望表单向下滚动以使该新添加的组件可见。

我假设.scrollComponentToVisible()使用了.scrollComponentToVisible() ,但我不为我工作。

如果运行下面提供的示例代码,您会注意到该组件已正确添加并获得了焦点。 但是,它仍然在屏幕的visible区域之外。

注意行:

form.scrollComponentToVisible(cont4);

我猜这行是错的吗? 我应该怎么用呢?

import javax.microedition.midlet.*;

import com.sun.lwuit.*;
import com.sun.lwuit.events.*;

public class ScrollTest extends MIDlet {

public void startApp() {

    Display.init(this);

    final Form form = new Form();
    form.getStyle().setBgColor(0xff0000);

    String text = "aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa" +
                "aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa";

    Container cont = new Container();
    cont.setFocusable(true);
    cont.getUnselectedStyle().setBgTransparency(0);
    cont.getSelectedStyle().setBgTransparency(100);
    TextArea area = new TextArea(text);
    area.setEditable(false);
    area.setFocusable(false);
    area.getUnselectedStyle().setBgTransparency(0);
    cont.addComponent(area);
    form.addComponent(cont);

    Container cont2 = new Container();
    cont2.setFocusable(true);
    cont2.getUnselectedStyle().setBgTransparency(0);
    cont2.getSelectedStyle().setBgTransparency(100);
    TextArea area2 = new TextArea(text);
    area2.setEditable(false);
    area2.setFocusable(false);
    area2.getUnselectedStyle().setBgTransparency(0);
    cont2.addComponent(area2);
    form.addComponent(cont2);

    Container cont3 = new Container();
    cont3.setFocusable(true);
    cont3.getUnselectedStyle().setBgTransparency(0);
    cont3.getSelectedStyle().setBgTransparency(100);
    TextArea area3 = new TextArea(text);
    area3.setEditable(false);
    area3.setFocusable(false);
    area3.getUnselectedStyle().setBgTransparency(0);
    cont3.addComponent(area3);
    form.addComponent(cont3);

    Command add = new Command("Add");
    form.addCommand(add);

    form.addCommandListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Container cont4 = new Container();
            cont4.setFocusable(true);
            cont4.getSelectedStyle().setBgColor(0xff0000);
            TextArea area4 = new TextArea("This should get focus and be visible when added");
            area4.setEditable(false);
            area4.setFocusable(false);
            cont4.getUnselectedStyle().setBgTransparency(0);
            cont4.getSelectedStyle().setBgTransparency(100);
            cont4.addComponent(area4);
            form.addComponent(cont4);
            form.repaint();

            cont4.requestFocus();
            form.scrollComponentToVisible(cont4);

        }
    });

    form.show();
}

public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void actionPerformed(ActionEvent ae) {
    notifyDestroyed();
}
}

如果您希望每次单击添加的容器都相同,则可以使用下面的示例代码中的List和自定义列表单元格渲染器。

列表的setScrollToSelected可用于焦点和滚动到最后添加的项目。

public class ScrollTest extends MIDlet {

private List myList;
private ListModel mListModel;
private int i = 0;

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

    Form form = new Form();
    form.setLayout(new BorderLayout());
    form.setScrollableY(false);
    form.getStyle().setBgColor(0xff0000);

    myList = new List();
    CustomListRenderer mRenderer = new CustomListRenderer();
    myList.setListCellRenderer(mRenderer);
    mListModel = new DefaultListModel();
    myList.setModel(mListModel);
    myList.setFixedSelection(List.FIXED_NONE_ONE_ELEMENT_MARGIN_FROM_EDGE);
    myList.setSmoothScrolling(true);
    myList.setScrollToSelected(true);
    form.addComponent(BorderLayout.CENTER, myList);

    Command add = new Command("Add");
    form.addCommand(add);

    form.addCommandListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            mListModel.addItem(" Item #" + (i++));
            myList.setSelectedIndex(mListModel.getSize() - 1);
            myList.setScrollToSelected(true);
        }
    });

    form.show();
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

private class CustomListRenderer extends Container implements
        ListCellRenderer {
    TextArea area;
    Label focus = new Label();

    public CustomListRenderer() {
        this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        area = new TextArea();
        area.setEditable(false);
        area.setFocusable(false);
        this.addComponent(area);
        this.getStyle().setBgColor(0xff0000);
        this.getSelectedStyle().setBgColor(0xff6600);
    }

    public Component getListCellRendererComponent(List list, Object value,
            int index, boolean isSelected) {
        area.setText(value.toString());
        return this;
    }

    public Component getListFocusComponent(List arg0) {
        focus.getStyle().setBgColor(0xff6600);
        return focus;
    }
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM