1

这是我第一次在这里提问。

我是 J2ME 的新手,现在我正在开发一个小型应用程序,但是当我想将数据显示到表格中时我遇到了问题。但是在 J2me 中不支持那里的表格,因为我知道另一种方式可以代表表格,例如通过 Canvas 或 CustomItem 创建表格。

在 Canvas 中,我可以画两条线,例如:

-----------------------
|
|
|
|

但我不知道如何获得 2 行的坐标,例如:

                         |
                         |
                         | 
                         |
                         |
--------------------------

两个在整个屏幕上绘制一个矩形,

我知道画线法有 4 个因子 x1、y1、x2、y2。

但我无法计算 x 点和 y 点在上面画两条线

我需要你帮我解释或举例

我的代码:

package test;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author J2MENewBie
 */
public class TableCanvasExample extends Canvas {
    private int cols=3;
    private int rows =50;
    protected void paint(Graphics g) {
        g.setColor(0x94b2ff);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        //draw two lines
        g.setColor(0xf8011e);
        g.drawLine(0, 0, 0, this.getWidth());
        g.drawLine(0, 0, this.getHeight(), 0);

    }

}

package test;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.*;

/**
 * @author J2ME NewBie
 */
public class TableCanvasMidlet extends MIDlet {
    private TableCanvasExample tbcve;

    public TableCanvasMidlet(){
        tbcve = new TableCanvasExample();
    }
    public void startApp() {
        Display.getDisplay(this).setCurrent(tbcve);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

P / s:垂直线没有全尺寸我不知道为什么?

谢谢!

4

1 回答 1

0

您的代码中有太多看起来相同的零 - 尝试使用描述性名称:

    int w = getWidth(), h = getHeight(); // I think this way it's easier to read

    int xLeft = 0, yTop = 0; // descriptive names for zeroes
    // below, replace w - 1 -> w and h - 1 -> h if lines drawn are off-by-one
    int xRight = w - 1, yBottom = h - 1; // names for top - right coordinates

    g.drawLine(xLeft, yTop, xLeft, yBottom); // your left vertical
    g.drawLine(xLeft, yTop, xRight, yTop); // your top horizontal

    g.drawLine(xRight, yTop, xRight, yBottom); // add right vertical
    g.drawLine(xLeft, yBottom, xRight, yBottom); // add bottom horizontal

如果绘制的矩形看起来不像您期望的那样在上面的代码中找到错误的语义

于 2011-09-23T15:59:11.307 回答