【发布时间】:2011-09-11 01:27:44
【问题描述】:
我正在编写我的第一个 LWUIT 和 Java ME 应用程序并尝试让按钮状态正常工作。我知道我可以在资源编辑器中做到这一点,但我真的很想知道为什么我的代码不起作用。我在下面的代码行为看似不规律。如果我选择第一个按钮,它工作正常。当我选择第二个按钮时,该按钮具有选定状态的前景,但未选定状态的背景。第三个按钮也是如此。但是,当我回到第一个按钮时,第一个按钮和第三个按钮都具有选中状态的背景,第一个按钮具有选中状态的前景,第三个按钮具有未选中状态的前景。 我试过阅读教程和在线论坛,但似乎大多数都已经过时了。甚至官方 LWUIT 页面上的教程也有一些命令已被弃用,以至于 Netbeans 将它们显示为未解决而不是弃用。我确定这是一个简单的错误,但我无法从这段代码中看到其他按钮应该如何受到被选中或未选中的影响,或者为什么每次按钮状态更改时选中和未选中的样式都会改变。
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);
这应该是所有相关代码,因为它是处理按钮的唯一部分,除了将按钮添加到容器和将容器添加到表单的 addComponent 调用。
【问题讨论】: