如何使用 JavaFX 建立顏色選擇器?


顏色選擇器提供了一個標準顏色面板,你可以從中選擇所需的顏色。你可以透過例項化 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) {
      //Setting the label
      Label label = new Label("Select Desired Color:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      label.setFont(font);
      //Creating a Color picker
      ColorPicker picker = new ColorPicker();
      //Creating a hbox to hold the pagination
      HBox hbox = new HBox();
      hbox.setSpacing(20);
      hbox.setPadding(new Insets(25, 50, 50, 60));
      hbox.getChildren().addAll(label, picker);
      //Setting the stage
      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);
   }
}

輸出

更新於: 2020 年 5 月 18 日

327 次閱讀

開啟你的職業生涯

完成本課程以獲得認證

開始
廣告
© . All rights reserved.