Copiable Label/TextField/LabeledText in JavaFX

后端 未结 2 1836
悲&欢浪女
悲&欢浪女 2020-11-29 09:46

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 su

2条回答
  •  眼角桃花
    2020-11-29 09:50

    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);
        }
    }
    

    and

    copyable-text.css:

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

提交回复
热议问题