JavaFX 中的可复制标签/TextField/LabeledText [英] Copiable Label/TextField/LabeledText in JavaFX

查看:45
本文介绍了JavaFX 中的可复制标签/TextField/LabeledText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在 JavaFX 中创建可复制的标签.我试图创建没有背景、没有焦点边框和默认背景颜色的 TextField,但我没有成功.我发现了很多关于如何从控件中删除焦点背景的问题,但所有这些看起来都像是黑客".

I just want to create copiable label in JavaFX. I have tried to create TextField that have no background, have no focus border and default background color, but I have no success. I have found a lot of questions how to remove focus background from control but all of that looks like "hacks".

是否有实现可复制文本的标准解决方案?

Is there are any standard solution to implement copyable text?

推荐答案

可以用css创建没有边框和背景色的TextField:

You can create a TextField without the border and background color with css:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CopyableLabel extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField copyable = new TextField("Copy this");
        copyable.setEditable(false);
        copyable.getStyleClass().add("copyable-label");

        TextField tf2 = new TextField();
        VBox root = new VBox();
        root.getChildren().addAll(copyable, tf2);
        Scene scene = new Scene(root, 250, 150);
        scene.getStylesheets().add(getClass().getResource("copyable-text.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

copyable-text.css:

copyable-text.css:

.copyable-label, .copyable-label:focused {
    -fx-background-color: transparent ;
    -fx-background-insets: 0px ;
}

这篇关于JavaFX 中的可复制标签/TextField/LabeledText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆