事情就是这样。但我需要返回颜色时,我按其他切换按钮
我需要做的是颜色需要回到蓝色后,点击另一个按钮(而切换另一个按钮需要不切换切换按钮)
问题是当我切换第一个按钮(工作正常-改变框上的颜色)。但当我按下第二个按钮,而选择了第一个按钮,第一个按钮颜色框的颜色没有返回蓝色。
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
}
}
1条答案
按热度按时间frebpwbc1#
你不需要设置
onAction
的属性ToggleButton
s。只需将changelistener添加到selectedToggle
财产ToggleGroup
. 在那里面ChangeListener
你需要做两件事:改变颜色
Rectangle
与新选择的ToggleButton
.还原先前选定的颜色
ToggleButton
.我根据你问题中的代码反向设计了一个fxml文件。下面的代码是一个sscce,只需对您的代码进行最小的更改,就可以显示如何使用
ChangeListener
.文件:fxmldocu.fxml
你的
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;
}
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 {
}