JavaFX在自定义操作事件上打开颜色选择器对话框

huangapple go评论83阅读模式
英文:

JavaFX open Color Picker's Dialog on Custom Action Event

问题

我想要启动颜色选择器的对话框窗口并获取所选颜色作为输出。
在我的情况下,我希望这个窗口是通过点击矩形对象而不是ColorPicker组件来启动的。

我在找出用于启动此窗口的类/方法方面遇到了困难,因为所有在线资源都只是指向我要避免的ColorPicker示例。
我尝试查阅文档,但没有任何运气,所以如果你们知道如何帮助我,我会非常感激你们。

英文:

I want to launch the Color Picker's Dialog window and get the selected color as output.
In my case I want this window to launch from clicking on a Rectangle object instead of the ColorPicker component.

I'm having trouble finding out what the class/method is for launching this window since all online resources just point me to ColorPicker examples which I want to avoid.
I tried checking the documentation without any luck so if you guys know how to help me I would be very grateful to you.

答案1

得分: 3

我认为你在询问如何在不实际显示颜色选择器控件的情况下使颜色选择器的弹出窗口出现。要使其工作,你需要将颜色选择器放在场景图中,但你可以将其设置为不可见。请注意,它仍将放置在场景图中,其位置将决定弹出窗口的位置。

以下是一个最小示例:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) {
        Rectangle rect = new Rectangle(100, 20, 200, 40);
        ColorPicker picker = new ColorPicker();
        picker.setVisible(false);
        Pane root = new Pane(rect, picker);
        rect.setStroke(Color.BLACK);
        rect.fillProperty().bind(picker.valueProperty());
        rect.setOnMouseClicked(e -> {
            // 移动(不可见的)颜色选择器,以使弹出窗口出现在鼠标位置:
            picker.relocate(e.getX(), e.getY() - picker.getHeight());
            picker.show();
        });
        Scene scene = new Scene(root, 400, 80);
        stage.setScene(scene);
        stage.show();
    }

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

这是启动时的外观:

JavaFX在自定义操作事件上打开颜色选择器对话框

这是点击矩形后的结果:

JavaFX在自定义操作事件上打开颜色选择器对话框

英文:

I think you are asking how to make the color picker's popup appear without actually showing the color picker control. You need to have the color picker in the scene graph for it to work; however you can just make it invisible. Note that it will still be placed in the scene graph and its location will determine the location of the popup.

Here's a minimal example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) {
        Rectangle rect = new Rectangle(100, 20, 200, 40);
        ColorPicker picker = new ColorPicker();
        picker.setVisible(false);
        Pane root = new Pane(rect, picker);
        rect.setStroke(Color.BLACK);
        rect.fillProperty().bind(picker.valueProperty());
        rect.setOnMouseClicked(e -> {
            // move the (invisible) color picker so the popup appears at the mouse:
            picker.relocate(e.getX(), e.getY() - picker.getHeight());
            picker.show();
        });
        Scene scene = new Scene(root, 400, 80);
        stage.setScene(scene);
        stage.show();
    }

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

Here is how this looks at startup:

JavaFX在自定义操作事件上打开颜色选择器对话框

and here is the result of clicking on the rectangle:

JavaFX在自定义操作事件上打开颜色选择器对话框

huangapple
  • 本文由 发表于 2023年8月4日 02:38:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定