Respuesta :
Answer:
package colorchanger;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class ColorChanger extends Application {
@Override
public void start(Stage primaryStage) {
//Create the two radio buttons
RadioButton radio1 = new RadioButton("White");
RadioButton radio2 = new RadioButton("Red");
//Create a toggle group for the buttons
ToggleGroup tg = new ToggleGroup();
//Add the two buttons to the toggle group
radio1.setToggleGroup(tg);
radio2.setToggleGroup(tg);
//Create an HBox to hold the two radio buttons
HBox hb = new HBox();
hb.getChildren().addAll(radio1, radio2);
hb.setPadding(new Insets(10));
hb.setSpacing(10);
hb.setPrefSize(30, 15);
//Set action for the two buttons
//When the radio1 button is clicked, the background color of the horizontal box (HBox) changes to white
radio1.setOnAction(e -> {
hb.setStyle("-fx-background-color:white");
});
//When the radio2 button is clicked, the background color of the horizontal box (HBox) changes to red
radio2.setOnAction(e -> {
hb.setStyle("-fx-background-color:red");
});
//Create a scene to hold the HBox
Scene scene = new Scene(hb, 300, 250);
//Create a primary stage to act as window
primaryStage.setTitle("Color Changer");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Explanation:
Go through the comments in the code for explanation.
The source code file has also been attached to this response.
Hope this helps!