java 颜色选择器_JavaFX 颜色选择器

JavaFX教程 - JavaFX颜色选择器

颜色选择器控件使用户能够从可用范围中选择颜色,或通过指定RGB或HSB组合设置附加颜色。

JavaFX ColorPicker控件具有颜色选择器,调色板和自定义颜色对话框窗口。

ColorPicker创建

以下代码使用空构造函数和创建颜色选择器控件颜色选择器控件使用默认颜色,即WHITE。ColorPicker colorPicker1 = new ColorPicker();

我们还可以提供颜色常量作为当前选择的颜色。ColorPicker colorPicker2 = new ColorPicker(Color.BLUE);

我们还可以提供网络颜色值作为当前选择的颜色ColorPicker colorPicker3 = new ColorPicker(Color.web("#EEEEEE"选择器));

例子import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.scene.Scene;

import javafx.scene.control.ColorPicker;

import javafx.scene.layout.HBox;

import javafx.scene.paint.Color;

import javafx.scene.text.Text;

import javafx.stage.Stage;

public class Main extends Application {

public static void main(String[] args) {

launch(args);

}

@Override

public void start(Stage stage) {

Scene scene = new Scene(new HBox(20), 400, 100);

HBox box = (HBox) scene.getRoot();

final ColorPicker colorPicker = new ColorPicker();

colorPicker.setValue(Color.RED);

final Text text = new Text("Color picker:");

text.setFill(colorPicker.getValue());

colorPicker.setOnAction((ActionEvent t) -> {

text.setFill(colorPicker.getValue());

});

box.getChildren().addAll(colorPicker, text);

stage.setScene(scene);

stage.show();

}

}

自定义颜色

getCustomColors()方法返回创建的自定义颜色Color对象的ObservableList。ObservableList customColors = colorPicker.getCustomColors();

colorPicker.setValue(customColors.get(index));

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaFX中的时间选择器主要有两种:DatePicker和DateTimePicker。 1. DatePicker DatePicker控件允许用户从一个日历中选择一个日期。以下是一个简单的示例: ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.DatePicker; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ DatePicker datePicker = new DatePicker(); VBox vBox = new VBox(datePicker); Scene scene = new Scene(vBox, 300, 200); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 2. DateTimePicker DateTimePicker控件允许用户从一个日历中选择一个日期和时间。由于JavaFX本身不提供DateTimePicker控件,因此需要使用第三方库。在这里,我们使用JFXtras库来实现DateTimePicker控件。以下是一个简单的示例: ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.VBox; import javafx.stage.Stage; import jfxtras.scene.control.LocalDateTimePicker; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ LocalDateTimePicker dateTimePicker = new LocalDateTimePicker(); VBox vBox = new VBox(dateTimePicker); Scene scene = new Scene(vBox, 300, 200); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值