javaFX使用枚舉實現html中下拉框功能


package sample;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import  javafx.scene.*;

import java.io.FileInputStream;
import java.net.URL;

/**
 * @author lq
 * 創建時間 2019/4/12 0:46
 **/
public class FXMLExample4 extends Application {

    enum  City {
        BEIJING(1,"北京"),SHANGHAI(2,"上海"),GUANGZHOU(3,"廣州");

        int id;
        String name;

        City() {

        }
        City(int id,String name){
            this.id = id;
            this.name = name;
        }

        @Override
        public String toString() {
            return this.name;
        }
    }

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

    ObservableList cursors = FXCollections.observableArrayList(
            City.BEIJING,
            City.SHANGHAI,
            City.GUANGZHOU
    );

    /**
     * 類似html下拉框,使用枚舉類型
     * @param primaryStage
     * @throws Exception
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

            ChoiceBox choiceBoxRef = ChoiceBoxBuilder.create()
                    .items(cursors)
                    .build();

            VBox box = new VBox();
            box.getChildren().add(choiceBoxRef);
            final Scene scene = new Scene(box,300, 250);
            scene.setFill(null);
            primaryStage.setScene(scene);
            primaryStage.show();

            choiceBoxRef.setOnAction(event -> {
                City value = (City) choiceBoxRef.getValue();
                System.out.println(value.id);
            });

        }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM