向表单添加组件时,我希望表单向下滚动以使新添加的组件可见。
我认为这.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();
}
}