如何使用 JavaFX 创建 ColorPicker?

javafxobject oriented programmingprogramming更新于 2025/4/14 8:22:17

颜色选择器为您提供标准调色板,您可以从中选择所需的颜色。您可以通过实例化 javafx.scene.control.ColorPicker 类 来创建颜色选择器。

示例

以下示例演示了如何创建 ColorPicker

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class ColorPickerExample extends Application {
   public void start(Stage stage) {
      //设置标签
      Label label = new Label("Select Desired Color:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      //创建颜色选择器
      ColorPicker picker = new ColorPicker();
      //创建水平框以容纳分页
      HBox hbox = new HBox();
      hbox.setSpacing(20);
      hbox.setPadding(new Insets(25, 50, 50, 60));
      hbox.getChildren().addAll(label, picker);
      //设置舞台
      Group root = new Group(hbox);
      Scene scene = new Scene(root, 595, 300, Color.BEIGE);
      stage.setTitle("Color Picker");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

输出


相关文章