我有一个 VBox
,位于多个 TextFlow
内。如何使该文本可选择/可复制?
public class ExampleController implements Initializable {
@FXML
private VBox box;
@Override
public void initialize(final URL location, final ResourceBundle resources) {
final TextFlow tf = new TextFlow();
final Text t0 = new Text("First part");
final Text t1 = new Text(", second");
final Text t2 = new Text(" and third");
t0.getStyleClass().add("first-part-styling");
t1.getStyleClass().add("second-part-styling");
t2.getStyleClass().add("third-part-styling");
tf.getChildren().addAll(t0, t1, t2);
this.box.getChildren().add(tf);
}
}
我使用 TextFlow
和 Text
,因为我需要在同一个句子中使用不同的样式。
最佳答案
您不能使用Text
,因为此类应该仅显示文本。但您可以使用 TextField
来代替:
public class JavaFXTest extends Application {
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
final TextFlow tf = new TextFlow();
final Text t0 = new Text("First part");
final Text t1 = new Text(", second");
final TextField t2 = new TextField(" and third");
t2.setEditable(false);
t2.setBackground(Background.EMPTY);
t2.setFocusTraversable(false);
tf.getChildren().addAll(t0, t1, t2);
root.getChildren().add(tf);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
编辑您可能会看一下 Rich-text area for JavaFX 。 GitHub 上的这个开源项目提供了一个可设置样式的文本区域。不过还没试过...
关于javafx - 如何从 TextFlow 复制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43757425/