Anonymous
Получение визуальных границ узла
Сообщение
Anonymous » 31 окт 2024, 19:09
Я пытаюсь создать собственную рамку для текстового поля. Я использовалboundsInParentProperty, чтобы создать границу. Однако эти границы ведут себя как ограничительная рамка, что не подходит для моего случая. Ниже приведен снимок текстового поля с рамкой:
Проблемы возникают при повороте узла. Ниже приведен еще один снимок при повороте текстового поля:
Это пример кода:
Код: Выделить всё
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.Background;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathElement;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
import org.jetbrains.annotations.NotNull;
public class CustomBorder extends Application {
@Override
public void start(Stage primaryStage) {
TextField textField = new TextField("Hello, World!");
textField.setStyle("-fx-padding: 8; -fx-max-width: 20; -fx-font-size: 18;" +
"-fx-border-insets: 0; -fx-background-insets: 0; -fx-background-radius: 0; " +
"-fx-border-style: none; -fx-background-color: rgba(255,0,0,0.5); -fx-min-width: 140");
textField.setRotate(-25);
Path border = new Path();
border.getElements().addAll(createBorder(textField, BorderLine.LEFT));
border.getElements().addAll(createBorder(textField, BorderLine.TOP));
border.getElements().addAll(createBorder(textField, BorderLine.RIGHT));
border.getElements().addAll(createBorder(textField, BorderLine.BOTTOM));
border.setManaged(false);
border.setViewOrder(0);
HBox root = new HBox(textField, border);
root.setBackground(Background.fill(Color.TRANSPARENT));
root.getTransforms().add(new Scale(2, 2));
root.relocate(100, 100);
primaryStage.setTitle("Custom Border");
primaryStage.setScene(new Scene(root, 500, 400));
primaryStage.show();
root.requestFocus();
}
@NotNull
private PathElement[] createBorder(Region region, @NotNull BorderLine borderLine) {
MoveTo moveTo = new MoveTo();
LineTo lineTo = new LineTo();
switch (borderLine) {
case LEFT -> region.boundsInParentProperty().addListener((observable, oldValue, newValue) -> {
moveTo.setX(newValue.getMinX());
moveTo.setY(newValue.getMinY());
lineTo.setX(newValue.getMinX());
lineTo.setY(newValue.getMaxY());
});
case TOP -> region.boundsInParentProperty().addListener((observable, oldValue, newValue) -> {
moveTo.setX(newValue.getMinX());
moveTo.setY(newValue.getMinY());
lineTo.setX(newValue.getMaxX());
lineTo.setY(newValue.getMinY());
});
case RIGHT -> region.boundsInParentProperty().addListener((observable, oldValue, newValue) -> {
moveTo.setX(newValue.getMaxX());
moveTo.setY(newValue.getMinY());
lineTo.setX(newValue.getMaxX());
lineTo.setY(newValue.getMaxY());
});
case BOTTOM -> region.boundsInParentProperty().addListener((observable, oldValue, newValue) -> {
moveTo.setX(newValue.getMinX());
moveTo.setY(newValue.getMaxY());
lineTo.setX(newValue.getMaxX());
lineTo.setY(newValue.getMaxY());
});
}
return new PathElement[]{moveTo, lineTo};
}
public enum BorderLine {
LEFT, RIGHT, TOP, BOTTOM;
}
public static void main(String[] args) {
launch(args);
}
}
Как сделать так, чтобы граница имела правильные значения x/y?
Подробнее здесь:
https://stackoverflow.com/questions/791 ... -of-a-node
1730390944
Anonymous
Я пытаюсь создать собственную рамку для текстового поля. Я использовалboundsInParentProperty, чтобы создать границу. Однако эти границы ведут себя как ограничительная рамка, что не подходит для моего случая. Ниже приведен снимок текстового поля с рамкой: [img]https://i.sstatic.net/8Mz2FS9T. png[/img] Проблемы возникают при повороте узла. Ниже приведен еще один снимок при повороте текстового поля: [img]https://i.sstatic.net/bm97nPOU.png[/img] Это пример кода: [code]import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.TextField; import javafx.scene.layout.Background; import javafx.scene.layout.HBox; import javafx.scene.layout.Region; import javafx.scene.paint.Color; import javafx.scene.shape.LineTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; import javafx.scene.shape.PathElement; import javafx.scene.transform.Scale; import javafx.stage.Stage; import org.jetbrains.annotations.NotNull; public class CustomBorder extends Application { @Override public void start(Stage primaryStage) { TextField textField = new TextField("Hello, World!"); textField.setStyle("-fx-padding: 8; -fx-max-width: 20; -fx-font-size: 18;" + "-fx-border-insets: 0; -fx-background-insets: 0; -fx-background-radius: 0; " + "-fx-border-style: none; -fx-background-color: rgba(255,0,0,0.5); -fx-min-width: 140"); textField.setRotate(-25); Path border = new Path(); border.getElements().addAll(createBorder(textField, BorderLine.LEFT)); border.getElements().addAll(createBorder(textField, BorderLine.TOP)); border.getElements().addAll(createBorder(textField, BorderLine.RIGHT)); border.getElements().addAll(createBorder(textField, BorderLine.BOTTOM)); border.setManaged(false); border.setViewOrder(0); HBox root = new HBox(textField, border); root.setBackground(Background.fill(Color.TRANSPARENT)); root.getTransforms().add(new Scale(2, 2)); root.relocate(100, 100); primaryStage.setTitle("Custom Border"); primaryStage.setScene(new Scene(root, 500, 400)); primaryStage.show(); root.requestFocus(); } @NotNull private PathElement[] createBorder(Region region, @NotNull BorderLine borderLine) { MoveTo moveTo = new MoveTo(); LineTo lineTo = new LineTo(); switch (borderLine) { case LEFT -> region.boundsInParentProperty().addListener((observable, oldValue, newValue) -> { moveTo.setX(newValue.getMinX()); moveTo.setY(newValue.getMinY()); lineTo.setX(newValue.getMinX()); lineTo.setY(newValue.getMaxY()); }); case TOP -> region.boundsInParentProperty().addListener((observable, oldValue, newValue) -> { moveTo.setX(newValue.getMinX()); moveTo.setY(newValue.getMinY()); lineTo.setX(newValue.getMaxX()); lineTo.setY(newValue.getMinY()); }); case RIGHT -> region.boundsInParentProperty().addListener((observable, oldValue, newValue) -> { moveTo.setX(newValue.getMaxX()); moveTo.setY(newValue.getMinY()); lineTo.setX(newValue.getMaxX()); lineTo.setY(newValue.getMaxY()); }); case BOTTOM -> region.boundsInParentProperty().addListener((observable, oldValue, newValue) -> { moveTo.setX(newValue.getMinX()); moveTo.setY(newValue.getMaxY()); lineTo.setX(newValue.getMaxX()); lineTo.setY(newValue.getMaxY()); }); } return new PathElement[]{moveTo, lineTo}; } public enum BorderLine { LEFT, RIGHT, TOP, BOTTOM; } public static void main(String[] args) { launch(args); } } [/code] Как сделать так, чтобы граница имела правильные значения x/y? Подробнее здесь: [url]https://stackoverflow.com/questions/79145255/getting-the-visual-bounds-of-a-node[/url]