我正在尝试使用切换按钮制作指示灯。当它的“Selected”值为真时,它会“照亮”。当它的“Selected”值为false时,而不是“illuminate”。我添加了常规按钮只是为了测试切换按钮上视觉效果的格式。在实际程序中,“灯”将在其他方法中进行控制。
问题是当我设置,toggleBtn.setSelected(true); 它不会更新视觉效果。如果我调用 toggleBtn.isSelected(); 它返回 true,所以我知道它正在更新,而不是视觉效果。如果我在单击“测试”按钮之前通过在程序中单击“预选”切换按钮,它将循环到关闭的视觉效果。但它不会循环回到 On 视觉效果。
我已经搜索过了,我认为似乎我需要添加一个侦听器,但是我不确定这将如何帮助更新视觉效果?
以下是代码的相关部分。如果您需要更多,请告诉我。
控制器
@FXML
private void LightsTest(ActionEvent event) throws InterruptedException {
System.out.println("Light illuminated");
toggleBtn.setSelected(true);
System.out.println("Light is Selected: " + toggleBtn.isSelected());
Thread.sleep(1000);
System.out.println("Light not illuminated");
toggleBtn.setSelected(false);
System.out.println("Light is Selected: " + toggleBtn.isSelected());
}
文件格式
<AnchorPane id="mainPane" prefHeight="600.0" prefWidth="1024.0" stylesheets="@CSSFile.css" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
<children>
<ToggleButton id="Light" fx:id="toggleBtn" mnemonicParsing="false" styleClass="warningLights" text="LIGHT ON" />
<Button id="testBtn" fx:id="lightsBtn" layoutX="427.0" layoutY="30.0" mnemonicParsing="false" onAction="#LightsTest" prefHeight="49.0" prefWidth="106.0" text="TEST LIGHTS" />
</children>
</AnchorPane>
CSS
.warningLights {
-fx-background-color: linear-gradient(#132233 5%, #465566 50%, #132233 95%);
-fx-background-radius: 15px;
-fx-border-width: 5px;
-fx-border-radius: 15px 15px 15px 15px;
-fx-font-size: 18px;
-fx-font-weight: bold;
}
.warningLights:selected {
-fx-background-color: linear-gradient(#EE0000 5%, #FDA8A8 50%, #EE0000 95%);
-fx-effect: dropshadow(three-pass-box, black, 10, 0.0, 1, 5);
}
谢谢大家!
倚天杖
相关分类