按钮图像问题

发布于 2024-08-02 09:31:40 字数 3567 浏览 4 评论 0原文

你好。我想在这里问你一件事。我知道这对你来说可能很容易,但对我来说可能很难。我学习新的J2ME。我想了解这门编程语言的基础知识。我有一个这样的案例:

class DrawImageCanvas extends Canvas {

    static Image image;
    static Image image2;
    static Image image3;
    static Image image4;
    static Image image5;
    static Image image6;
    static String string; 

    int count;

    public void paint(Graphics g) {
        int width = getWidth();
        int height = getHeight();

        // Fill the background using black
        g.setColor(0xefffff);
        g.fillRect(0, 0, width, height);

        // Load an image from the MIDlet resources
        if (image == null && image2 == null && image3 == null && image4 == null && image5 == null && image6 == null) {
            try {
                image = Image.createImage("/http.png");
                image2 = Image.createImage("/Back.png");
                image3 = Image.createImage("/next.png");
                image4 = Image.createImage("/refresh.png");
                image5 = Image.createImage("/stop.png");
                image6 = Image.createImage("/Bookmark.png");
                string = new String("Disini tempat halaman web ditampilkan");
            } catch (IOException ex) {
                g.setColor(0xffffff);
                g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
                return;
            }
        }


         g.drawImage(image, 5, 5, Graphics.TOP | Graphics.LEFT);    
         g.drawImage(image2, 90, 5, Graphics.TOP | Graphics.LEFT);
         g.drawImage(image3, 120, 5, Graphics.TOP | Graphics.LEFT);
         g.drawImage(image4, 150, 5, Graphics.TOP | Graphics.LEFT);
         g.drawImage(image5, 180, 5, Graphics.TOP | Graphics.LEFT);
         g.drawImage(image6, 210, 5, Graphics.TOP | Graphics.LEFT);
         g.drawString(string, 5, 36, Graphics.TOP | Graphics.LEFT);
    }
}

我想知道如何将图像更改为图像按钮(按键功能)。请帮助


我尝试添加带有按键事件的代码,但这让我感到困惑,

然后我尝试添加图形但是..我认为它只在画布上使用,

这是我的新代码,这是与最后结果相似的结果

public void startApp() {
    display = Display.getDisplay(this);
    exit = new Command("Exit", Command.EXIT, 1);
    form = new Form("");    
    form.addCommand(exit);
    form.setCommandListener(this);   
    try { 
      Image image = Image.createImage("/http.png");
      Image image2 = Image.createImage("/Back.png");
      Image image3 = Image.createImage("/next.png");
      Image image4 = Image.createImage("/refresh.png");
      Image image5 = Image.createImage("/stop.png");
      Image image6 = Image.createImage("/Bookmark.png");
      form.append(new ImageItem(null, image, ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT, null));
      form.append(new ImageItem(null, image2, ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT, null));
      form.append(new ImageItem(null, image3, ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT, null));
      form.append(new ImageItem(null, image4, ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT, null));
      form.append(new ImageItem(null, image5, ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT, null)); 
      form.append(new ImageItem(null, image6, ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_NEWLINE_AFTER, null));   

      display.setCurrent(form);
    }catch (java.io.IOException e){
      System.err.println(e);
    }
    display.setCurrent(form);
  }

,但问题是。 .我不知道如何将图像制作为按钮。 我是 J2me 的新手请帮忙

hallo. I want to ask something to you here. I know this may be easy for you, but this may be difficult for me. I learn new J2ME. I want to know the basics of the programing languages this. I have a case like this:

class DrawImageCanvas extends Canvas {

    static Image image;
    static Image image2;
    static Image image3;
    static Image image4;
    static Image image5;
    static Image image6;
    static String string; 

    int count;

    public void paint(Graphics g) {
        int width = getWidth();
        int height = getHeight();

        // Fill the background using black
        g.setColor(0xefffff);
        g.fillRect(0, 0, width, height);

        // Load an image from the MIDlet resources
        if (image == null && image2 == null && image3 == null && image4 == null && image5 == null && image6 == null) {
            try {
                image = Image.createImage("/http.png");
                image2 = Image.createImage("/Back.png");
                image3 = Image.createImage("/next.png");
                image4 = Image.createImage("/refresh.png");
                image5 = Image.createImage("/stop.png");
                image6 = Image.createImage("/Bookmark.png");
                string = new String("Disini tempat halaman web ditampilkan");
            } catch (IOException ex) {
                g.setColor(0xffffff);
                g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
                return;
            }
        }


         g.drawImage(image, 5, 5, Graphics.TOP | Graphics.LEFT);    
         g.drawImage(image2, 90, 5, Graphics.TOP | Graphics.LEFT);
         g.drawImage(image3, 120, 5, Graphics.TOP | Graphics.LEFT);
         g.drawImage(image4, 150, 5, Graphics.TOP | Graphics.LEFT);
         g.drawImage(image5, 180, 5, Graphics.TOP | Graphics.LEFT);
         g.drawImage(image6, 210, 5, Graphics.TOP | Graphics.LEFT);
         g.drawString(string, 5, 36, Graphics.TOP | Graphics.LEFT);
    }
}

I want to know how do I change the image to be a image button (key function). please help


i have tried to add code with key press event but it make me confused

then i try add GRAPHIC but.. i think it only used at canvas

this is my new code, this is simmilar result as last result

public void startApp() {
    display = Display.getDisplay(this);
    exit = new Command("Exit", Command.EXIT, 1);
    form = new Form("");    
    form.addCommand(exit);
    form.setCommandListener(this);   
    try { 
      Image image = Image.createImage("/http.png");
      Image image2 = Image.createImage("/Back.png");
      Image image3 = Image.createImage("/next.png");
      Image image4 = Image.createImage("/refresh.png");
      Image image5 = Image.createImage("/stop.png");
      Image image6 = Image.createImage("/Bookmark.png");
      form.append(new ImageItem(null, image, ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT, null));
      form.append(new ImageItem(null, image2, ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT, null));
      form.append(new ImageItem(null, image3, ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT, null));
      form.append(new ImageItem(null, image4, ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT, null));
      form.append(new ImageItem(null, image5, ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT, null)); 
      form.append(new ImageItem(null, image6, ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_LEFT | ImageItem.LAYOUT_NEWLINE_AFTER, null));   

      display.setCurrent(form);
    }catch (java.io.IOException e){
      System.err.println(e);
    }
    display.setCurrent(form);
  }

but the problem is.. i dont know how to make image as button.
i am newbie in J2me please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画离情绘悲伤 2024-08-09 09:31:40

您可以保留按钮的某种数据结构来指示遍历顺序。然后,您可以监听画布上的按键事件,并根据按下的键(例如向左、向右、向上、向下、选择等),您可以更新现在处于焦点的按钮(使用简单的索引到您的数据结构)。您的绘制方法可以扩展为在具有焦点的图像周围绘制某种边框。

现在,当您聆听选择按键时,您将知道哪个按钮具有焦点,从而知道要执行哪个操作。

这里更高级的问题是每次按键后刷新(重新绘制屏幕)以更新图像等以显示哪个具有焦点。您不想重绘整个屏幕,而是可以根据将要更改的按钮(即以前具有焦点的按钮和现在具有焦点的按钮)来定义要重新绘制的屏幕区域。

另一种选择是不使用低级画布类,而是考虑使用像 LWUIT 这样的框架,它提供按钮等和布局管理器,就像 Swing 一样。这可能比尝试使用低级 API 进行绘制(从外观上看是浏览器)要容易得多。

You could keep some sort of data structure of your buttons indicating a traversal order. You could then listen for the key pressed events on your canvas and based on which key was pressed (e.g. left, right, up, down, select etc) you could update which of the buttons is now in focus (using a simple index into your data structure). Your paint method could be extended to draw a border of some sort around the image that has the focus.

Now when you listen for the select key press you will know which button has the focus and therefore which action to perform.

More advanced issues here would be refreshing (repainting the screen) after each key press to update the image etc to show which has focus. You wouldn't want to redraw the entire screen, instead you can define areas of the screen to repaint based on which buttons will have changed (i.e. the button that previously had focus and the button that now has focus).

Another alternative would be to not use the low level canvas class but instead look at using a framework like LWUIT which provides buttons etc. and layout managers, much like Swing. This might be a lot easier than trying to draw (a browser by the looks of it) using the low level API.

随风而去 2024-08-09 09:31:40

您无法在 J2ME 中的 Canvas 上创建按钮,但有其他选择:

  • 使用 javax.microedition.lcdui.Form,向其添加外观为 BUTTON 的 ImageItem,向其添加 Command ImageItem。

  • 通过覆盖 Canvas.keyPressed()、Canvas.keyreleased()、Canvas.pointerPressed() 和/或 Canvas.pointerReleased() 在 DrawImageCanvas 上使用按键和/或指针事件。您甚至可以向图像添加边框,使其看起来更像按钮。

  • 您可能想尝试在 Form 中使用 javax.microedition.lcdui.CustomItem 作为中间解决方案。

MIDP 规范中为所有这些类和方法提供了不错的文档:

http:// /www.jcp.org/en/jsr/detail?id=118

您可能还想查看 LWUIT 的文档。它是Sun公司发布的一个开源图形库。

You can't create a button on a Canvas in J2ME but there are alternatives:

  • Use a javax.microedition.lcdui.Form, add a ImageItem with appearance BUTTON to it, add a Command to the ImageItem.

  • Use key and/or pointer events on your DrawImageCanvas by overiding Canvas.keyPressed(), Canvas.keyreleased(), Canvas.pointerPressed() and/or Canvas.pointerReleased(). You can even add a border to the image so it looks more like a button.

  • You might want to experiment with a javax.microedition.lcdui.CustomItem inside a Form as an intermediary solution.

There is decent documentation for all these classes and methods in the MIDP specification:

http://www.jcp.org/en/jsr/detail?id=118

You might also want to look at the documentation of LWUIT. It is an open source graphical library published by Sun.

三生池水覆流年 2024-08-09 09:31:40

尝试将ImageItem对象的外观模式设置为Item.BUTTON,例如

ImageItem item = new ImageItem(null, image, ImageItem.LAYOUT_LEFT, null, Item.BUTTON);

还尝试向ImageItem添加命令。

item.setDefaultCommand(new Command("Back", Command.ITEM, 1); 

最后定义CommandListener:

item.setItemCommandListener(/* itemCommandListener */);

Try setting the appearance mode of the ImageItem object to Item.BUTTON, e.g.

ImageItem item = new ImageItem(null, image, ImageItem.LAYOUT_LEFT, null, Item.BUTTON);

Also try adding a command to the ImageItem.

item.setDefaultCommand(new Command("Back", Command.ITEM, 1); 

Then finally define the CommandListener:

item.setItemCommandListener(/* itemCommandListener */);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文