【问题标题】:How to make a Javafx Label selectable如何使 Javafx 标签可选
【发布时间】:2017-10-25 17:00:44
【问题描述】:

在 JavaFx8 中是否可以选择标签文本?我知道,还有其他简单的解决方法,例如使用 TextField。但是我的标签需要带有 TextField 不提供的换行功能的多行文本。如果我使用 TextArea,问题是我不能像标签一样根据文本大小缩小 TextArea。所以我不能使用它们中的任何一个。

我对标签文本的使用如下:

<VBox>
    <Label wrapText="true"
           VBox.vgrow="ALWAYS"
           maxHeight="Infinity" minHeight="-Infinity"
           text="Some Random Subject Line With Very Large Text To Test The Wrap Text, Lorem Ipsum Dolor"/>                       
</VBox>

根据 VBox 的宽度,Label 的高度会调整大小以完全适合文本。我无法使用 TextArea 或 TextField 模拟这种行为。但我需要能够从标签中选择文本。有什么想法吗?

【问题讨论】:

  • 我不认为这是可能的,或者至少这是 oracle 的this 线程所建议的。看起来这是一个要求在未来版本中实现的功能。
  • 那么任何人制作的任何开源自定义控件?
  • @Developer66 在我的问题中已经提到,这个技巧对我不起作用,因为 TextField 不支持多行换行文本。它既不会像 label 那样根据文本长度调整自身大小。
  • 肯定可以使用父 VBox 的布局设置进行配置?

标签: java css javafx label


【解决方案1】:

这是一个解决方法,直到有人发布更好的东西。

如果您双击标签,它会变为 TextArea。然后,您可以复制文本。在 TextArea 上按 Enter 后,它会变回标签。

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication110 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        VBox root = new VBox();

        StackPane stackpane = new StackPane();        

        Label label = new Label("Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world");
        VBox.setVgrow(label, Priority.ALWAYS);
        label.wrapTextProperty().set(true);

        label.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
                    if(mouseEvent.getClickCount() == 2){
                        label.setVisible(false);
                        TextArea textarea = new TextArea(label.getText());
                        textarea.setPrefHeight(label.getHeight() + 10);
                        stackpane.getChildren().add(textarea);

                        textarea.setOnKeyPressed(event ->{
                            System.out.println(event.getCode());
                            if(event.getCode().toString().equals("ENTER"))
                            {
                                stackpane.getChildren().remove(textarea);
                                label.setVisible(true);                               
                            }
                        });
                    }
                }
            }
        });

        stackpane.getChildren().add(label);   

        root.getChildren().add(stackpane);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 2014-10-23
    • 2017-05-12
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多