javafx-如何正确配置多个切换按钮(在一个组中)动作以满足我的需要

gpnt7bae  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(387)

事情就是这样。但我需要返回颜色时,我按其他切换按钮
我需要做的是颜色需要回到蓝色后,点击另一个按钮(而切换另一个按钮需要不切换切换按钮)
问题是当我切换第一个按钮(工作正常-改变框上的颜色)。但当我按下第二个按钮,而选择了第一个按钮,第一个按钮颜色框的颜色没有返回蓝色。

public class FXMLDocumentController implements Initializable {

    @FXML
    private ToggleButton TB1;

    @FXML
    private ToggleGroup G1;

    @FXML
    private ToggleButton TB2;

    @FXML
    private ToggleButton TB3;

    @FXML
    private ToggleButton TB4;

    @FXML
    private Rectangle C1;

    @FXML
    private Rectangle C2;

    @FXML
    private Rectangle C3;

    @FXML
    private Rectangle C4;

    @FXML
    void TB1Action(ActionEvent event) {
        if (TB1.isSelected()){
            FillTransition ft = new FillTransition(Duration.millis(250), C1, Color.DODGERBLUE, Color.RED);
            ft.play();
        }
        else{
            FillTransition ft = new FillTransition(Duration.millis(250), C1, Color.RED, Color.DODGERBLUE);
            ft.play();
        }
    }

    @FXML
    void TB2Action(ActionEvent event) {
        if (TB2.isSelected()){
            FillTransition ft = new FillTransition(Duration.millis(250), C2, Color.DODGERBLUE, Color.GREENYELLOW);
            ft.play();
        }
        else{
            FillTransition ft = new FillTransition(Duration.millis(250), C2, Color.GREENYELLOW, Color.DODGERBLUE);
            ft.play();
        }
    }

    @FXML
    void TB3Action(ActionEvent event) {
        if (TB3.isSelected()){
            FillTransition ft = new FillTransition(Duration.millis(250), C3, Color.DODGERBLUE, Color.GREENYELLOW);
            ft.play();
        }
        else{
            FillTransition ft = new FillTransition(Duration.millis(250), C3, Color.GREENYELLOW, Color.DODGERBLUE);
            ft.play();
        }
    }

    @FXML
    void TB4Action(ActionEvent event) {
        if (TB4.isSelected()){
            FillTransition ft = new FillTransition(Duration.millis(250), C4, Color.DODGERBLUE, Color.GREENYELLOW);
            ft.play();
        }
        else{
            FillTransition ft = new FillTransition(Duration.millis(250), C4, Color.GREENYELLOW, Color.DODGERBLUE);
            ft.play();
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
}
frebpwbc

frebpwbc1#

你不需要设置 onAction 的属性 ToggleButton s。只需将changelistener添加到 selectedToggle 财产 ToggleGroup . 在那里面 ChangeListener 你需要做两件事:
改变颜色 Rectangle 与新选择的 ToggleButton .
还原先前选定的颜色 ToggleButton .
我根据你问题中的代码反向设计了一个fxml文件。下面的代码是一个sscce,只需对您的代码进行最小的更改,就可以显示如何使用 ChangeListener .
文件:fxmldocu.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.paint.Color?>
<?import javafx.scene.shape.Rectangle?>

<VBox fx:id="root"
      xmlns:fx="https://javafx.com/fxml/1"
      xmlns="http://javafx.com/javafx/15.0.1"
      fx:controller="FXMLDocumentController"
      spacing="10">
    <fx:define>
        <ToggleGroup fx:id="G1"/>
    </fx:define>
   <children>
      <HBox spacing="10">
         <children>
            <ToggleButton fx:id="TB1" text="ToggleButton" toggleGroup="$G1" selected="false">
            </ToggleButton>
            <Rectangle fx:id="C1" width="100" height="29">
               <fill><Color fx:constant="DODGERBLUE"/></fill>
            </Rectangle>
         </children>
      </HBox>
      <HBox spacing="10">
         <children>
            <ToggleButton fx:id="TB2" text="ToggleButton" toggleGroup="$G1" selected="false">
            </ToggleButton>
            <Rectangle fx:id="C2" width="100" height="29">
               <fill><Color fx:constant="DODGERBLUE"/></fill>
            </Rectangle>
         </children>
      </HBox>
      <HBox spacing="10">
         <children>
            <ToggleButton fx:id="TB3" text="ToggleButton" toggleGroup="$G1" selected="false">
            </ToggleButton>
            <Rectangle fx:id="C3" width="100" height="29">
               <fill><Color fx:constant="DODGERBLUE"/></fill>
            </Rectangle>
         </children>
      </HBox>
      <HBox spacing="10">
         <children>
            <ToggleButton fx:id="TB4" text="ToggleButton" toggleGroup="$G1" selected="false">
            </ToggleButton>
            <Rectangle fx:id="C4" width="100" height="29">
               <fill><Color fx:constant="DODGERBLUE"/></fill>
            </Rectangle>
         </children>
      </HBox>
   </children>
</VBox>

你的 FXMLDocumentController 添加 ChangeListener ```
import javafx.animation.FillTransition;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;

public class FXMLDocumentController {
@FXML
private ToggleButton TB1;

@FXML
private ToggleGroup G1;

@FXML
private ToggleButton TB2;

@FXML
private ToggleButton TB3;

@FXML
private ToggleButton TB4;

@FXML
private Rectangle C1;

@FXML
private Rectangle C2;

@FXML
private Rectangle C3;

@FXML
private Rectangle C4;

@FXML
void TB1Action(ActionEvent event) {
    if (TB1.isSelected()){
        FillTransition ft = new FillTransition(Duration.millis(250), C1, Color.DODGERBLUE, Color.RED);
        ft.play();
    }
    else{
        FillTransition ft = new FillTransition(Duration.millis(250), C1, Color.RED, Color.DODGERBLUE);
        ft.play();
    }
}

@FXML
void TB2Action(ActionEvent event) {
    if (TB2.isSelected()){
        FillTransition ft = new FillTransition(Duration.millis(250), C2, Color.DODGERBLUE, Color.GREENYELLOW);
        ft.play();
    }
    else{
        FillTransition ft = new FillTransition(Duration.millis(250), C2, Color.GREENYELLOW, Color.DODGERBLUE);
        ft.play();
    }
}

@FXML
void TB3Action(ActionEvent event) {
    if (TB3.isSelected()){
        FillTransition ft = new FillTransition(Duration.millis(250), C3, Color.DODGERBLUE, Color.GREENYELLOW);
        ft.play();
    }
    else{
        FillTransition ft = new FillTransition(Duration.millis(250), C3, Color.GREENYELLOW, Color.DODGERBLUE);
        ft.play();
    }
}

@FXML
void TB4Action(ActionEvent event) {
    if (TB4.isSelected()){
        FillTransition ft = new FillTransition(Duration.millis(250), C4, Color.DODGERBLUE, Color.GREENYELLOW);
        ft.play();
    }
    else{
        FillTransition ft = new FillTransition(Duration.millis(250), C4, Color.GREENYELLOW, Color.DODGERBLUE);
        ft.play();
    }
}

@FXML
private void initialize() {
    G1.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {

        @Override
        public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
            if (oldValue == TB1) {
                TB1Action(null);    
            }
            else if (oldValue == TB2) {
                TB2Action(null);
            }
            else if (oldValue == TB3) {
                TB3Action(null);
            }
            else if (oldValue == TB4) {
                TB4Action(null);
            }
            if (newValue == TB1) {
                TB1Action(null);    
            }
            else if (newValue == TB2) {
                TB2Action(null);
            }
            else if (newValue == TB3) {
                TB3Action(null);
            }
            else if (newValue == TB4) {
                TB4Action(null);
            }
        }
    });
}

}

注意,“controller”类不需要实现 `Initializable` 接口。它可以简单地声明 `initialize` 方法,正如我在上面的代码中所做的那样。
最后,一个 `Application` 类来运行应用程序。

import java.net.URL;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Togglers extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
    Class<?> theClass = getClass();
    URL url = theClass.getResource("fxmldocu.fxml");
    VBox root = (VBox) FXMLLoader.load(url);
    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}

}

另请注意,另一种可能的替代方法-我没有探讨-可能是绑定 `selectedToggle` 财产 `ToggleGroup` 与 `selected` 每个的属性 `ToggleButton` .
最后,(在kleopatra添加一个关于它的注解之前:-)考虑使用java命名约定。

相关问题