开发者

Setting button states in LWUIT

I'm writing my first LWUIT and Java ME application and trying to get button states to work. I know I can do it in the resource editor, but I'd really like to know why my code isn't working. The code I have below behaves seemingly erratically. If I select the first button, it works fine. When I select the second button, that button has the foreground of the selected stated, but the background of the unselected state. The same goes for the third button. However, when I wrap back around to the first button, both the first button开发者_JS百科 and the third button have the background of the selected state, the first button has the foreground of the selected state and the third button has the foreground of the unselected state. I've tried reading tutorials and online forums, but it seems that most of those are horribly out of date. Even the tutorial on the official LWUIT page has commands in it that are so deprecated that Netbeans shows them as unresolved instead of deprecated. I'm sure it's a simple mistake, but I just can't see from this code how the other buttons should be affected by one getting selected or unselected, or why the style for selected and unselected gets changed every time a buttons state changes.

    Style buttonUp = new Style();
    buttonUp.setAlignment(Component.CENTER);
    buttonUp.setBgColor(0x0082ff);
    buttonUp.setFgColor(0xffffff);
    buttonUp.setMargin(5,5,0,0);

    Style buttonDown = new Style();
    buttonDown.setAlignment(Component.CENTER);
    buttonDown.setBgColor(0xd7d7ee);
    buttonDown.setFgColor(0x000000);
    buttonDown.setMargin(5,5,0,0);        

    Container buttons = new Container(new BoxLayout(BoxLayout.Y_AXIS));
    Button firstButton = new Button("first");
    firstButton.setUnselectedStyle(buttonUp);
    firstButton.setSelectedStyle(buttonDown);
    firstButton.setPressedStyle(buttonDown);

    Button secondButton = new Button("second");
    secondButton.setUnselectedStyle(buttonUp);
    secondButton.setSelectedStyle(buttonDown);
    secondButton.setPressedStyle(buttonDown);

    Button thirdButton = new Button("third");
    thirdButton.setUnselectedStyle(buttonUp);
    thirdButton.setSelectedStyle(buttonDown);
    thirdButton.setPressedStyle(buttonDown);

This should be all of the relevant code as it is the only part that deals with the buttons, other than the addComponent calls that adds the buttons to the container and the container to the form.


You are reusing style object instances which isn't legal each component state must have a singular instance. It is more common in LWUIT to do this by:

button.getUnselectedStyle().setFgColor(...);

Alternatively you can implement the logic in a method:

updateButtonTheme(Style);

and invoke it as:

updateButtonTheme(button.getUnselectedStyle());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜