【问题标题】:Setting button states in LWUIT在 LWUIT 中设置按钮状态
【发布时间】: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 调用。

【问题讨论】:

    标签: java-me lwuit


    【解决方案1】:

    您正在重用不合法的样式对象实例,每个组件状态都必须有一个单独的实例。在 LWUIT 中更常见的做法是:

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

    或者,您可以在方法中实现逻辑:

    updateButtonTheme(Style);
    

    并将其调用为:

    updateButtonTheme(button.getUnselectedStyle());
    

    【讨论】:

    • 谢谢。出于某种奇怪的原因,当我在上面的代码中为按钮添加边框时,情况自行解决了。我确信“修复”实际上是一个小故障,因为添加边框应该没有区别。我已经按照您的建议调整了代码,现在可以正常工作了。
    猜你喜欢
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多