Java黑皮书16.1(使用单选按钮)
package sample;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Text text = new Text(200, 150, "我是一行文本");
text.setFont(new Font("宋体", 40));
pane.getChildren().add(text);
pane.setStyle("-fx-border-color: gray");
RadioButton radioButton_Red = new RadioButton("Red");
RadioButton radioButton_Yellow = new RadioButton("Yellow");
RadioButton radioButton_Black = new RadioButton("Black");
RadioButton radioButton_Orange = new RadioButton("Orange");
RadioButton radioButton_Green = new RadioButton("Green");
ToggleGroup toggleGroup = new ToggleGroup();
radioButton_Red.setToggleGroup(toggleGroup);
radioButton_Yellow.setToggleGroup(toggleGroup);
radioButton_Black.setToggleGroup(toggleGroup);
radioButton_Black.setSelected(true);
radioButton_Orange.setToggleGroup(toggleGroup);
radioButton_Green.setToggleGroup(toggleGroup);
HBox hBox1 = new HBox(5);
hBox1.getChildren().addAll(radioButton_Red, radioButton_Yellow, radioButton_Black, radioButton_Orange, radioButton_Green);
hBox1.setAlignment(Pos.CENTER);
Button button_Left = new Button("<=");
Button button_Right = new Button("=>");
HBox hBox2 = new HBox(5);
hBox2.getChildren().addAll(button_Left, button_Right);
hBox2.setAlignment(Pos.CENTER);
BorderPane borderPane = new BorderPane();
borderPane.setCenter(pane);
borderPane.setTop(hBox1);
borderPane.setBottom(hBox2);
Scene scene = new Scene(borderPane, 600, 300);
primaryStage.setTitle("使用单选按钮");
primaryStage.setScene(scene);
primaryStage.show();
radioButton_Red.setOnAction(e -> text.setStroke(Color.RED));
radioButton_Yellow.setOnAction(e -> text.setStroke(Color.YELLOW));
radioButton_Black.setOnAction(e -> text.setStroke(Color.BLACK));
radioButton_Orange.setOnAction(e -> text.setStroke(Color.ORANGE));
radioButton_Green.setOnAction(e -> text.setStroke(Color.GREEN));
button_Left.setOnAction(e -> text.setX(text.getX() - 10));
button_Right.setOnAction(e -> text.setX(text.getX() + 10));
}
public static void main(String[] args) { launch(args); }
}
运行结果