1
我正在使用LWUIT中的固定標題的某個表上工作。讓我直接來解決這個問題。當我在仿真器上運行它時,觸摸界面工作得很好,並且窗體的滾動工作完美無瑕,但是當我嘗試用導航鍵導航時,按下的左鍵會產生一個小故障。該故障是,當我瀏覽到右側,然後回到左邊,第2列左側滾動fezzes,它不會移動到極左無論多少次,我按下左鍵。經過許多努力,我發現了一個組合,隨機按住UP和LEFT鍵不放,然後窗體向左滾動。我不知道爲什麼會發生這種情況。我在所有的模擬器和2個手機上都試過,但仍然是同樣的問題。會不會有一些代碼造成這種問題?我不確定這一點。任何幫助,將不勝感激。此外,我發佈了整個源代碼,以便任何感興趣的人可以嘗試它,並可以看到這個問題是否出現在他的模擬器上。表滾動不按預期方式工作
import com.sun.lwuit.Button;
import com.sun.lwuit.Component;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.table.DefaultTableModel;
import com.sun.lwuit.table.Table;
import com.sun.lwuit.table.TableModel;
import javax.microedition.lcdui.Canvas;
import javax.microedition.midlet.MIDlet;
public class FixedTableDemo extends MIDlet implements ActionListener {
private static final String[] TITLES = {"Title 1", "Title 2", "Title 3", "Title 4", "Title 5"};
private static final int ROWS = 100;
private static Dimension[] TITLE_SIZES;
Button b[][];
static class MirroredTable extends Table {
private MirroredTable mirrorTo;
public MirroredTable(TableModel m) {
super(m);
}
public MirroredTable(TableModel m, boolean b) {
super(m, b);
}
public void setScrollX(int x) {
super.setScrollX(x);
if(isDragActivated()) {
mirrorTo.setScrollX(x);
}
}
/**
* @param mirrorTo the mirrorTo to set
*/
public void setMirrorTo(MirroredTable mirrorTo) {
this.mirrorTo = mirrorTo;
}
public Component createCell(Object value, int row, int column, boolean editable) {
Component c = super.createCell(value, row, column, editable);
if(TITLE_SIZES != null && TITLE_SIZES[column] != null) {
c.setPreferredSize(TITLE_SIZES[column]);
}
return c;
}
}
MirroredTable titlesTable;
MirroredTable bodyTable;
public void startApp() {
Display.init(this);
Form f = new Form("Table Title");
titlesTable = new MirroredTable(new DefaultTableModel(TITLES, new Object[0][0]));
//TITLE_SIZES = new Dimension[TITLES.length];
/*for(int iter = 0 ; iter < TITLES.length ; iter++) {
*
* TITLE_SIZES[iter] = titlesTable.createCell(TITLES[iter], -1, iter, false).getPreferredSize();
*
* }*/
titlesTable.removeAll();
Button bt[]=new Button[5];
for(int i=0;i<5;i++)
{
bt[i]=new Button("Title "+i);
bt[i].setUIID("");
titlesTable.addComponent(bt[i]);
}
Object[][] body = new Object[ROWS][TITLES.length];
/* for(int rows = 0 ; rows < body.length ; rows++) {
*
* for(int cols = 0 ; cols < body[rows].length ; cols++) {
*
* body[rows][cols] = "" + rows + ", " + cols;
*
* }
*
* }*/
bodyTable = new MirroredTable(new DefaultTableModel(TITLES, body), false);
bodyTable.setMirrorTo(titlesTable);
titlesTable.setMirrorTo(bodyTable);
bodyTable.setScrollable(true);
titlesTable.setScrollableX(true);
f.setScrollable(false);
f.setLayout(new BorderLayout());
titlesTable.setTensileDragEnabled(false);
bodyTable.setTensileDragEnabled(false);
titlesTable.setIsScrollVisible(false);
f.addComponent(BorderLayout.NORTH, titlesTable);
f.addComponent(BorderLayout.CENTER, bodyTable);
f.addGameKeyListener(Canvas.LEFT, this);
f.addGameKeyListener(Canvas.RIGHT, this);
f.addGameKeyListener(Canvas.UP, this);
f.setCyclicFocus(false);
/**
* buttons adding and their listener
*/
b=new Button[50][5];
//Label l[][]=new Label[50][5];
for(int i=0;i<50;i++)
{
for(int j=0; j<5;j++)
{
b[i][j]=new Button("Title "+i);
b[i][j].setUIID("");
//l[i][j]=new Label("Title 1");
bodyTable.addComponent(b[i][j]);
}
}
f.show();
}
public void actionPerformed(ActionEvent evt) {
if(evt.getKeyEvent()==Canvas.LEFT)
{
System.out.println(bodyTable.getScrollX());
titlesTable.setScrollX(bodyTable.getScrollX());
}
else if(evt.getKeyEvent()==Canvas.RIGHT)
{
System.out.println(bodyTable.getScrollX());
titlesTable.setScrollX(bodyTable.getScrollX());
}
else if(evt.getKeyEvent()==Canvas.UP)
{
System.out.println("Up:"+bodyTable.getScrollX()+","+bodyTable.getScrollY());
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
感謝,它的工作完美 –