JavaFX选择框

JavaFX选择框允许用户在几个选项之间快速选择。

创建一个选择框

我们可以使用ChoiceBox中的构造函数来创建ChoiceBox对象。

以下代码显示了如何使用其构造函数创建和填充选择框。 列表项是从可观察的列表来创建的。

ChoiceBox cb =newChoiceBox(FXCollections.observableArrayList("A","B","C")); 

我们还可以使用一个空的选择框使用它的默认构造函数,并使用setItems方法设置列表项。

ChoiceBox cb = new ChoiceBox();
cb.setItems(FXCollections.observableArrayList(
    "A", "B", new Separator(), "C", "D")
);

上面的代码还向选择框中添加了一个分隔符对象。分隔符分隔控件项目。

例子1

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Main extends Application {

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

    @Override
    public void start(Stage stage) {
        Group root = new Group();

        ChoiceBox<String> cb = new ChoiceBox<String>(FXCollections.observableArrayList("a", "b", "c", "d", "e"));
        //默认值
        cb.setValue("a");

        root.getChildren().add(cb);

        Scene scene = new Scene(root,300,300);
        stage.setScene(scene);
        stage.show();

    }
}

例子2

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Main extends Application {
    
    
    final Label label = new Label("Hello");
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage stage) {
        Rectangle rect = new Rectangle(150, 30);
        Scene scene = new Scene(new Group());
        scene.setFill(Color.ALICEBLUE);
        stage.setScene(scene);
        stage.show();
        
        stage.setWidth(300);
        stage.setHeight(200);
        
        Label label = new Label("A");
        label.setStyle("-fx-font: 25 arial;");
        label.setLayoutX(40);
        
        rect.setStroke(Color.BLUE);
        rect.setStrokeWidth(3);
        rect.setFill(Color.WHITE);
        
        final String[] greetings = new String[] { "A", "B", "C", "D", "E" };
        final ChoiceBox<String> cb = new ChoiceBox<String>(
            FXCollections.observableArrayList("a", "b", "c", "d", "e"));
        
        cb.getSelectionModel().selectedIndexProperty()
            .addListener(new ChangeListener<Number>() {
                public void changed(ObservableValue ov, Number value, Number new_value) {
                    label.setText(greetings[new_value.intValue()]);
                }
            });
        
        cb.setTooltip(new Tooltip("Select the language"));
        cb.setValue("English");
        HBox hb = new HBox();
        hb.getChildren().addAll(cb, label);
        hb.setSpacing(30);
        hb.setAlignment(Pos.CENTER);
        hb.setPadding(new Insets(10, 0, 0, 10));
        
        ((Group) scene.getRoot()).getChildren().add(hb);
        
    }
}

此处为语雀视频卡片,点击链接查看:Video_2022-04-28_161022.wmv

例子3

以下代码显示了如何在ChoiceBox中填充数据。


import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {
    ObservableList cursors = FXCollections.observableArrayList(
            Cursor.DEFAULT,
            Cursor.CROSSHAIR,
            Cursor.WAIT,
            Cursor.TEXT,
            Cursor.HAND,
            Cursor.MOVE,
            Cursor.N_RESIZE,
            Cursor.NE_RESIZE,
            Cursor.E_RESIZE,
            Cursor.SE_RESIZE,
            Cursor.S_RESIZE,
            Cursor.SW_RESIZE,
            Cursor.W_RESIZE,
            Cursor.NW_RESIZE,
            Cursor.NONE
    );
    @Override
    public void start(Stage stage) {
        ChoiceBox choiceBoxRef = new ChoiceBox(cursors);

        VBox box = new VBox();
        box.getChildren().add(choiceBoxRef);
        final Scene scene = new Scene(box,300, 250);
        scene.setFill(null);
        stage.setScene(scene);
        stage.show();
        scene.cursorProperty().bind(choiceBoxRef.getSelectionModel()
                .selectedItemProperty());

    }

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

此处为语雀视频卡片,点击链接查看:Video_2022-04-28_161022.wmv

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值