JavaFX、演示Label的属性

package fx;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Exercise16_15 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Label label = new Label("image", new ImageView("img/card_K.png"));  //标签
        String[] position = {"LEFT", "RIGHT", "TOP", "BOTTOM", "CENTER"};   //位置数组
        ComboBox<String> contentDisplay = new ComboBox<>(FXCollections.observableArrayList(position));  //内容展示
        contentDisplay.setOnAction(event -> {   //复选框注册动作事件
            String value = contentDisplay.getItems().get(contentDisplay.getItems().indexOf(contentDisplay.getValue()));
            switch (value) {
                case "TOP": label.setContentDisplay(ContentDisplay.TOP); break;
                case "RIGHT": label.setContentDisplay(ContentDisplay.RIGHT); break;
                case "BOTTOM": label.setContentDisplay(ContentDisplay.BOTTOM); break;
                case "LEFT": label.setContentDisplay(ContentDisplay.LEFT); break;
                case "CENTER": label.setContentDisplay(ContentDisplay.CENTER); break;
                default:
            }
        });

        TextField graphicTextGap = new TextField(); //结点文本间距
        graphicTextGap.setPrefColumnCount(5);   //设置列数
        graphicTextGap.setOnAction(event -> {
            try {
                label.setGraphicTextGap(Double.parseDouble(graphicTextGap.getText()));
            } catch (Exception ex) {
            }
        });

        HBox hBox = new HBox(10, new Label("contentDisplay:"), contentDisplay, new Label("graphicTextGap:"), graphicTextGap);
        hBox.setAlignment(Pos.CENTER);
        BorderPane pane = new BorderPane(new StackPane(label));
        pane.setTop(hBox);
        pane.setPadding(new Insets(5));
        pane.getCenter().setStyle("-fx-pref-height: 200px;");

        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Exercise16_15");
        primaryStage.show();
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值