Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javafx 2 围绕JavaFX文本节点绘制边框_Javafx 2 - Fatal编程技术网

Javafx 2 围绕JavaFX文本节点绘制边框

Javafx 2 围绕JavaFX文本节点绘制边框,javafx-2,Javafx 2,我想在javafx场景图中的任意javafx.scene.text.text节点周围绘制红色边框,例如按钮对象中的节点 检索所有文本节点很容易,但找不到它们在场景中的位置,它们的x和y属性似乎设置不正确,但它们没有宽度和高度 到目前为止,我已经尝试将带有红色笔划的矩形添加到堆栈窗格中,但x和y总是错误的,我无法获得大小。一个解决方案是在布局窗格中包装文本节点(例如HBox),并在布局窗格上使用CSS: import javafx.application.Application; import j

我想在javafx场景图中的任意javafx.scene.text.text节点周围绘制红色边框,例如按钮对象中的节点

检索所有文本节点很容易,但找不到它们在场景中的位置,它们的x和y属性似乎设置不正确,但它们没有宽度和高度


到目前为止,我已经尝试将带有红色笔划的矩形添加到堆栈窗格中,但x和y总是错误的,我无法获得大小。

一个解决方案是在布局窗格中包装文本节点(例如HBox),并在布局窗格上使用CSS:

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class TextBorderExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        final HBox root = new HBox(5);

        root.getChildren().addAll(
                new Text("This"), new Text("Is"), new Text("A"), createBorderedText("Red"), new Text("Bordered"), new Text("Text")
        );

        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
    private Node createBorderedText(String text) {
        final HBox hbox = new HBox();
        hbox.getChildren().add(new Text(text));
        hbox.setStyle("-fx-border-color: red;");
        return hbox ;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
另一种方法是使用矩形,如下所示:

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class TextBorderExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        final HBox root = new HBox(5);
        final Text red = new Text("Red");
        final Rectangle redBorder = new Rectangle(0, 0, Color.TRANSPARENT);
        redBorder.setStroke(Color.RED);
        redBorder.setManaged(false);
        red.boundsInParentProperty().addListener(new ChangeListener<Bounds>() {

            @Override
            public void changed(ObservableValue<? extends Bounds> observable,
                    Bounds oldValue, Bounds newValue) {
                redBorder.setLayoutX(red.getBoundsInParent().getMinX());
                redBorder.setLayoutY(red.getBoundsInParent().getMinY());
                redBorder.setWidth(red.getBoundsInParent().getWidth());
                redBorder.setHeight(red.getBoundsInParent().getHeight());
            }

        });
        root.getChildren().addAll(new Text("This"), new Text("Is"), new Text("A"), red, new Text("Bordered"), new Text("Text"));
        root.getChildren().add(redBorder);


        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
导入javafx.application.application;
导入javafx.beans.value.ChangeListener;
导入javafx.beans.value.observeValue;
导入javafx.geometry.Bounds;
导入javafx.scene.scene;
导入javafx.scene.layout.HBox;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Rectangle;
导入javafx.scene.text.text;
导入javafx.stage.stage;
公共类TextBorderExample扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
最终HBox根=新HBox(5);
最终文本红色=新文本(“红色”);
最终矩形红边=新矩形(0,0,颜色.透明);
红色边框。设定行程(颜色。红色);
redBorder.setManaged(false);
red.boundsInParentProperty().addListener(新的ChangeListener()){
@凌驾

public void已更改(ObservalEvalue非常感谢,boundsInParentProperty是关键。我仍然有一个小问题,位置是父对象的位置,而不是在场景中,但我认为现在要解决这个问题非常简单。