Java 表格滚动未按预期工作

Java 表格滚动未按预期工作,java,java-me,lwuit,lwuit-form,Java,Java Me,Lwuit,Lwuit Form,我正在使用LWUIT中的固定标题处理某个表。让我直接谈谈这个问题。当我在emulator上运行它时,触摸界面工作得非常好,表单的滚动工作得完美无缺,但是当我尝试使用导航键导航时,按下左键会产生故障。问题是,当我向右导航然后再向左导航时,左滚动条在第二列显示,无论我按多少次左键,它都不会移动到最左边。经过多次努力,我发现了一个组合,即连续随机按向上和向左键,然后表单滚动到最左边。我不明白为什么会这样。我在所有的模拟器和两个实际的手机上都试过了,但仍然存在同样的问题。是否有一些代码会造成这种问题?我

我正在使用LWUIT中的固定标题处理某个表。让我直接谈谈这个问题。当我在emulator上运行它时,触摸界面工作得非常好,表单的滚动工作得完美无缺,但是当我尝试使用导航键导航时,按下左键会产生故障。问题是,当我向右导航然后再向左导航时,左滚动条在第二列显示,无论我按多少次左键,它都不会移动到最左边。经过多次努力,我发现了一个组合,即连续随机按向上和向左键,然后表单滚动到最左边。我不明白为什么会这样。我在所有的模拟器和两个实际的手机上都试过了,但仍然存在同样的问题。是否有一些代码会造成这种问题?我不确定。在此方面的任何帮助都将不胜感激。此外,我还发布了整个源代码,以便任何感兴趣的人都可以原样尝试,看看这个问题是否出现在他的模拟器上

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) {

 }

}
import com.sun.lwuit.Button;
导入com.sun.lwuit.Component;
导入com.sun.lwuit.Display;
导入com.sun.lwuit.Form;
导入com.sun.lwuit.Label;
导入com.sun.lwuit.events.ActionEvent;
导入com.sun.lwuit.events.ActionListener;
导入com.sun.lwuit.geom.Dimension;
导入com.sun.lwuit.layouts.BorderLayout;
导入com.sun.lwuit.table.DefaultTableModel;
导入com.sun.lwuit.table.table;
导入com.sun.lwuit.table.TableModel;
导入javax.microedition.lcdui.Canvas;
导入javax.microedition.midlet.midlet;
公共类FixedTableDemo扩展MIDlet实现ActionListener{
私有静态最终字符串[]TITLES={“Title 1”、“Title 2”、“Title 3”、“Title 4”、“Title 5”};
私有静态最终整数行=100;
私有静态维度[]标题大小;
按钮b[][];
静态类MirroredTable扩展表{
私人镜像可供镜像;
公共镜像表(表格模型m){
超级(m);
}
公共镜像表(表模型m,布尔b){
super(m,b);
}
公共无效设置crollx(int x){
super.setScrollX(x);
如果(isDragActivated()){
反射镜。设置crollx(x);
}
}
/**
*@param mirror要设置的镜像
*/
公共无效设置镜像到(镜像可镜像到){
this.mirrorTo=mirrorTo;
}
公共组件createCell(对象值、整数行、整数列、布尔可编辑){
组件c=super.createCell(值、行、列、可编辑);
if(TITLE\u size!=null和TITLE\u size[列]!=null){
c、 setPreferredSize(标题_尺寸[列]);
}
返回c;
}
}
可镜像标题表;
可反光床身;
公开作废startApp(){
Display.init(this);
表格f=新表格(“表格标题”);
TitleTable=新的镜像表(新的DefaultTableModel(标题,新对象[0][0]);
//TITLE_size=新尺寸[TITLES.length];
/*对于(int-iter=0;iter对于(inti=0;i我不知道这是否是实现这一点的准确方法。但我对您的代码做了一些更改。
看看吧

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) {
}
}
import com.sun.lwuit.Button;
导入com.sun.lwuit.Component;
导入com.sun.lwuit.Display;
导入com.sun.lwuit.Form;
导入com.sun.lwuit.events.ActionEvent;
导入com.sun.lwuit.events.ActionListener;
导入com.sun.lwuit.geom.Dimension;
导入com.sun.lwuit.layouts.BorderLayout;
导入com.sun.lwuit.table.DefaultTableModel;
导入com.sun.lwuit.table.table;
导入com.sun.lwuit.table.TableModel;
导入javax.microedition.lcdui.Canvas;
导入javax.microedition.midlet.midlet;
公共类FixedTableDemo扩展MIDlet实现ActionListener{
私有静态最终字符串[]TITLES={“Title 1”、“Title 2”、“Title 3”、“Title 4”、“Title 5”};
私有静态最终整数行=100;
私有静态维度[]标题大小;
按钮b[][];
int屏幕宽度;
int lastMove=0;
int lastModifiedMove=1;
内部温度;
静态类MirroredTable扩展表{
私人镜像可供镜像;
公共镜像表(表格模型m){
超级(m);
}
公共镜像表(表模型m,布尔b){
super(m,b);
}
公共无效设置crollx(int x){
super.setScrollX(x);
如果(isDragActivated()){
反射镜。设置crollx(x);
}
}
/**
*
*@param mirror要设置的镜像
*
*/
公共无效设置镜像到(镜像可镜像到){
this.mirrorTo=mirrorTo;
}
公共组件createCell(对象值、整数行、整数列、布尔可编辑){
组件c=super.createCell(值、行、列、可编辑);
if(TITLE\u size!=null和TITLE\u size[列]!=null){
c、 setPreferredSize(标题_尺寸[列]);
}
返回c;
}
}
可镜像标题表;
可反光床身;
公开作废startApp(){
Display.init(this);
screenWidth=Display.getInstance().getDisplayWidth();
表格f=新表格(“表格标题”);
TitleTable=新的镜像表(新的DefaultTableModel(标题,新对象[0][0]);
可标题。删除所有();
按钮bt[]=新按钮[5];
对于(int i=0;i<5;i++){
bt[i]=新按钮(“标题”+(i+10));
bt[i].setUIID(“”);
TitleTable.addComponent(bt[i]);
}
Object[][]body=新对象[行][标题.长度];
bodyTable=新的MirroredTable(新的DefaultTableModel(标题,正文),false);
可调节的。可调节的(可调节的);
标题表。setMirrorTo(车身表);
bodyTable.setScrollable(真实);
标题表。设置Crollablex(真);
f、 设置可滚动(假);
f、 setLayout(新的BorderLayout());
TitleTable.setTensileDragEnabled(false);
bodyTable.setTensileDragEnabled(假);
标题表。setIsScrollVisible(假);
f、 addComponent(BorderLayout.NO