Java黑皮书16.1(使用单选按钮)

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); }
}

运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值