JavaFX选择框

时间:2025-04-02 10:23:00
  • import ;
  • import .value.ChangeListener;
  • import .value.ObservableValue;
  • import ;
  • import ;
  • import ;
  • import .Group;
  • import ;
  • import .control.ChoiceBox;
  • import .control.Label;
  • import .control.Tooltip;
  • import ;
  • import ;
  • import ;
  • import ;
  • public class Main extends Application {
  • Rectangle rect = new Rectangle(150, 30);
  • final Label label = new Label("Hello");
  • public static void main(String[] args) {
  • launch(args);
  • }
  • @Override
  • public void start(Stage stage) {
  • Scene scene = new Scene(new Group());
  • ();
  • (scene);
  • ();
  • (300);
  • (200);
  • ("-fx-font: 25 arial;");
  • X(40);
  • ();
  • (3);
  • ();
  • final String[] greetings = new String[] { "A", "B", "C", "D", "E" };
  • final ChoiceBox<String> cb = new ChoiceBox<String>(
  • ("a", "b", "c", "d", "e"));
  • ().selectedIndexProperty()
  • .addListener(new ChangeListener<Number>() {
  • public void changed(ObservableValue ov, Number value, Number new_value) {
  • (greetings[new_value.intValue()]);
  • }
  • });
  • (new Tooltip("Select the language"));
  • ("English");
  • HBox hb = new HBox();
  • ().addAll(cb, label);
  • (30);
  • ();
  • (new Insets(10, 0, 0, 10));
  • ((Group) ()).getChildren().add(hb);
  • }
  • }