gpt4 book ai didi

java - 在 JavaFX 中同时启用/禁用组按钮

转载 作者:行者123 更新时间:2023-11-29 08:20:58 25 4
gpt4 key购买 nike

我想在 JavaFX 中禁用多个相同“类型”的按钮。例如,我有多个用于不同操作的取消按钮,但我想同时启用/禁用它们,但似乎我无法在 LinkedList 中访问它们。

在下面的“最小且可重现”示例中,当按下开始时,它应该启用(在 fxml 中默认禁用)取消按钮,但它没有。

DemoController.java

public class DemoController {
@FXML
Button startBtn;
@FXML
Button cancelBtn1; // disabled by default
@FXML
Button cancelBtn2; // disabled by default
@FXML
Tab tab1;

public SDK sdk;

List<Node> cancelBtnGroup = new LinkedList<Node>();

public void initialize() {
cancelBtnGroup.add(cancelBtn1);
cancelBtnGroup.add(cancelBtn2);

startBtn.setOnAction(event -> {
tab1.setDisable(true);
startBtn.setDisable(true);
System.out.println("Disabling cancel buttons");
HashMap<String, String> test = new HashMap<String, String>();
test.put("disableCancel", "false");
sdk.onEvent(test);
});

initializeSDK();
}

public void initializeSDK() {
sdk = new SDK() {
@Override
public void onEvent(HashMap<String, String> e) {
if(e.containsKey("disableCancel")) {
for(Node btn : cancelBtnGroup) {
btn.setDisable(Boolean.parseBoolean(e.get("disableCancel")));
}
}
}
};
}
}

似乎我正在以某种方式覆盖按钮,但我不知道如何操作。我觉得我不了解某些功能的范围。我试图使代码尽可能简化和可读,所以如果我遗漏了一些非常明显的东西,这可能就是原因。 SDK 对象不包含具有相同名称的对象,为了确保这不是我更改名称的问题。

最佳答案

根据您发布的代码,我无法确定它不起作用的原因。但是,这是一个使用禁用共同祖先方法和 FXML 的最小示例:

App.java

package com.example;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class App extends Application {

@Override
public void start(Stage primaryStage) throws IOException {
var root = FXMLLoader.<Parent>load(getClass().getResource("/App.fxml"));
var scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}

App.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<VBox xmlns="http://javafx.com/javafx/13" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.example.Controller" spacing="10" prefWidth="500" prefHeight="300"
alignment="CENTER">

<padding>
<Insets topRightBottomLeft="10"/>
</padding>

<ToggleButton fx:id="toggleBtn"/>

<HBox fx:id="container" alignment="CENTER" spacing="10">
<Button text="Button #1"/>
<Button text="Button #2"/>
<Button text="Button #3"/>
<Button text="Button #4"/>
</HBox>

</VBox>

Controller.java

package com.example;

import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.HBox;

public class Controller {

@FXML private ToggleButton toggleBtn;
@FXML private HBox container;

@FXML
private void initialize() {
toggleBtn.textProperty().bind(
Bindings.when(toggleBtn.selectedProperty())
.then("Disable Buttons")
.otherwise("Enable Buttons")
);
container.disableProperty().bind(toggleBtn.selectedProperty().not());
}
}

注意:除了在代码中绑定(bind)容器的 disable 属性之外,还可以使用以下方法完成上述操作:

<!-- Everything else omitted for brevity -->
<HBox disable="${!toggleBtn.selected}">

当然,如果您使用 ToggleButton 以外的东西,那么在 FXML 中实现绑定(bind)可能不可行。

关于java - 在 JavaFX 中同时启用/禁用组按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58511260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com