我在LWUIT中使用固定标题的某个表上工作。让我直截了当地谈谈这个问题。当我在模拟器上运行它时,触摸界面工作非常精细并且表单的滚动工作完美无瑕,但是当我尝试使用导航键导航它时,按下的左键创建了一个小故障。故障是当我向右导航然后向左导航时,左侧滚动在第二列上发出颤动,无论我按下左键多少次,它都不会向左移动。经过多次努力,我找到了一个组合,在连续按下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) {
}
}
答案 0 :(得分:1)
我不知道这是否是实现这一目标的准确方法。但我在你的代码中做了一些改动。 看看这个。
import com.sun.lwuit.Button;
import com.sun.lwuit.Component;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
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[][];
int screenWidth;
int lastMove = 0;
int lastModifiedMove = 1;
int temp;
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);
screenWidth = Display.getInstance().getDisplayWidth();
Form f = new Form("Table Title");
titlesTable = new MirroredTable(new DefaultTableModel(TITLES, new Object[0][0]));
titlesTable.removeAll();
Button bt[] = new Button[5];
for (int i = 0; i < 5; i++) {
bt[i] = new Button("Title" + (i + 10));
bt[i].setUIID("");
titlesTable.addComponent(bt[i]);
}
Object[][] body = new Object[ROWS][TITLES.length];
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 + 10));
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) {
if (lastMove != 0) {
lastMove -= 20;
bodyTable.setScrollX(lastMove);
titlesTable.setScrollX(lastMove);
}
} else if (evt.getKeyEvent() == Canvas.RIGHT) {
if (lastMove != lastModifiedMove) {
lastMove += 20;
bodyTable.setScrollX(lastMove);
titlesTable.setScrollX(lastMove);
if (temp == bodyTable.getScrollX()) {
lastModifiedMove = temp;
lastMove = temp;
} else {
temp = bodyTable.getScrollX();
}
} else {
lastMove = lastModifiedMove;
}
} else if (evt.getKeyEvent() == Canvas.UP) {
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}