简体   繁体   中英

How to make buttons on a canvas in J2ME?

I'm new to J2ME and I was trying to make a page which has some logos(images) at the top, two buttons arranged side-by-side(one and only one of which is always selected) and a table displaying data below the buttons. The data is displayed according to which button is currently selected. I'm extending canvas and I was able to make the tables and draw the images but I could'nt find anything on making buttons on the internet. So how should I go about it? If anyone could point me to some sample code, that would be great too!

Not that hard, but you need to draw everything yourself. That means, buttons are either using a plain rectangle with text in the middle (different colours could indicate which is highlighted), or they could be images which you made yourself.

If they are arranged in a square, you need to keep track of which button is selected, and then change the selected index whenever a direction key is pressed.

iirc my favorite way to paint buttons on canvas was drawing and filling rounded rectangles. There are methods in Graphics package that do that; to get desired effect I just called them with same or off-by-one parameters I don't recall exactly.

  • note if you are going to write text over "buttons", consider using Font.getHeight and Font.stringWidth to determine size of rectangle to draw around

...logos(images) at the top, two buttons arranged side-by-side(one and only one of which is always selected) and a table displaying data below the buttons

BTW is there a reason why you don't use Form ? I ask because StringItem objects, created with appearance BUTTON and having ItemCommandListener look and behave like buttons but are easier to code. The rest also looks doable with Form , one just may need CustomItem to display the table like you describe

The default UI does not have buttons, only commands. You have to make one yourself or use a GUI framework like LWUit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM