[英]Javafx- how this multiple toggle buttons (in one group) actions are correctly configure for my need
This is what happens.这就是发生的事情。 But i need to return colors when i press other toggle buttons
但是当我按下其他切换按钮时,我需要返回 colors
What I need to do is colors need to be back to blue after clicking on another button (while toggling another button need to untoggle toggled buttons)我需要做的是 colors 在单击另一个按钮后需要恢复为蓝色(同时切换另一个按钮需要取消切换按钮)
Problem is when I toggle 1st button (working correctly - changing color on box).问题是当我切换第一个按钮时(正常工作 - 改变盒子上的颜色)。 But when I press 2nd button while selected 1st button, 1st button color box color not returning to blue.
但是当我在选择第一个按钮时按下第二个按钮时,第一个按钮颜色框颜色不会恢复为蓝色。
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
}
}
You don't need to set onAction
property for the ToggleButton
s.您不需要为
ToggleButton
设置onAction
属性。 Just add a ChangeListener to the selectedToggle
property of the ToggleGroup
.只需将ChangeListener添加到
ToggleGroup
的selectedToggle
属性。 In that ChangeListener
you need to do two things:在那个
ChangeListener
中你需要做两件事:
Rectangle
associated with the newly selected ToggleButton
.ToggleButton
关联的Rectangle
的颜色。ToggleButton
.ToggleButton
的颜色。 I reverse engineered a FXML file based on the code in your question.我根据您问题中的代码对 FXML 文件进行了逆向工程。 The below code is a SSCCE with minimal changes to your code just to show how to use a
ChangeListener
.下面的代码是一个SSCCE ,对您的代码进行了最小的更改,只是为了展示如何使用
ChangeListener
。
File: fxmldocu.fxml文件: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>
Your FXMLDocumentController
with added ChangeListener
您的
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);
}
}
});
}
}
Note that the "controller" class does not need to implement Initializable
interface.请注意,“控制器”class 不需要实现
Initializable
接口。 It can simply declare a initialize
method instead, as I have done in the above code.它可以简单地声明一个
initialize
方法,就像我在上面的代码中所做的那样。
Finally, an Application
class for running the application.最后,一个
Application
class 用于运行应用程序。
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);
}
}
Also note that another possible alternative – which I did not explore – could be to bind the selectedToggle
property of ToggleGroup
with the selected
property for each ToggleButton
.另请注意,另一种可能的替代方法(我没有探索)可能是将 ToggleGroup的
selectedToggle
属性与每个ToggleButton
的selected
属性ToggleGroup
。
Lastly, (and before kleopatra adds a comment regarding it:-) consider using Java naming conventions .最后,(在kleopatra添加评论之前:-)考虑使用Java 命名约定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.