- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想画一条垂直线,这条垂直线可能与文本对象相交。当我这样做而不做任何修改时,该行直接穿过单词,使单词难以阅读。我想使该行在到达文本时消失,并在文本节点之后立即继续。我试过使用带有 css 背景的 toBack() 使文本在其周围形成一个正方形,但似乎文本节点具有透明背景,因此该行在字母后面仍然可见。是否有另一种方法可以使文本不与线相交?请注意,文本可能一直存在也可能不存在,并且由于我程序的性质,它可以位于任何坐标,所以我不能只画两条线(一条在文本之前,一条在文本之后)。
最佳答案
使用 Label
而不是 Text
会使事情变得更容易。 Label
是(最终)Region
的子类,因此它具有可以设置样式的背景颜色等属性,使其具有不透明背景。使用 CSS 规则
-fx-background-color: -fx-background ;
在标签上会给它默认的背景颜色。相比之下,Text
是 Shape
的子类,只有 fill
和 stroke
等属性,它们为分别构成文本的字形的内部和外部。
所以你可以这样做
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class TextVsLabel extends Application {
@Override
public void start(Stage primaryStage) {
StackPane labelPane = new StackPane();
labelPane.setMinSize(250, 250);
Label label = new Label("A Label");
label.setStyle("-fx-background-color: -fx-background;");
labelPane.getChildren().add(label);
addLineToPane(labelPane);
StackPane textPane = new StackPane();
textPane.setMinSize(250, 250);
textPane.getChildren().add(new Text("A Text"));
addLineToPane(textPane);
HBox root = new HBox(5, labelPane, textPane);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
private void addLineToPane(Pane pane) {
Line line = new Line();
line.startXProperty().bind(pane.widthProperty().divide(2));
line.endXProperty().bind(line.startXProperty());
line.setStartY(0);
line.endYProperty().bind(pane.heightProperty());
// add line to beginning of pane's parent list, so it appears
// behind everything else
pane.getChildren().add(0, line);
}
public static void main(String[] args) {
launch(args);
}
}
结果
请注意,如果您需要在标签中的文本周围留出更多空间,您可以在标签中添加填充:-fx-padding: 1em;
等
关于css - 如何在看不到节点内的线的情况下绘制与文本节点相交的线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46184293/
我有一个标记的个体列表(Mark 列),这些个体在河流(LocStart 和 LocEnd)范围内的不同年份(Year 列)被捕获。在河上的位置以米为单位。 我想知道一个被标记的个体是否在不同年份之间
我目前正在开发一个事件管理系统,其中数据库存储时隙,如下所示: SlotId | DateTime | Duration -------+-------------------
给定同一个圆的两个圆段:A=[a1, a2] 和 B=[b1, b2],其中: a1、a2、b1、b2 的值介于 -inf 和 +inf 之间 a1 overlap A=[ -45°, 45
试图让两个数据集相交,但我做不到。例如,在我下面的代码中,相交 mySet 和 mySet2 应该产生“1”,因为它们在它们的集合中都有一个值“1”。 var mySet = new Set(); v
给定同一个圆的两个圆段:A=[a1, a2] 和 B=[b1, b2],其中: a1、a2、b1、b2 的值介于 -inf 和 +inf 之间 a1 overlap A=[ -45°, 45
我有两个要相交的集合,并对匹配元素执行求和运算。 例如集合是(在伪代码中): col1 = { {"A", 5}, {"B", 3}, {"C", 2} } col2 = { {"B", 1}, {"
我有一个使用 -setFrameRotation 旋转的 NSView。 (这是必要的,因为 View 响应鼠标事件,如果您仅使用旋转的 NSAffineTransform 绘制 View ,则不会获
我在网上找到了这段代码,显然它对其他人有效,但对我无效?我不知道哪里错了。我做了一个简单的例子,并将我的 Range1 和 Range 2 设为 excel 中的某些单元格, 另外,我想知道是否有办法
确定直线是否与矩形相交的最有效方法是什么? 我正在寻找类似的东西: CGPoint startLine = CGPointMake(5.0f,5.0f); CGPoint endLine = CGPo
QPolygonF有与其他 QPolygonF 并集、相交和相减的方法,但我需要与 QLineF 执行相交测试。 API 中似乎缺少此功能。 我想我可以做这样的事情: if (polygon .con
所以,我尝试使用矩形在游戏中对墙壁进行碰撞,我决定尝试使用 ArrayList 来存储每面墙的矩形,然后我将整个 field 设为一面墙,并且所有我想做的是删除三堵墙,所以我正在执行 shapeLis
鉴于这两个表/集合具有不同的项目组, 我如何找到 set1 中的哪些组跨越 set2 中的多个组? 如何找到 set1 中的组不能被 set2 中的单个组覆盖? 例如对于下表,A (1,2,5) 是唯
我在 Hive 中有两个字符串数组,例如 {'value1','value2','value3'} {'value1', 'value2'} 我想合并没有重复的数组,结果: {'value1','va
谁能给我 tsql 来查找包含起始日期和截止日期的日期。 select * from empc where DateFrom >= p_todate AND DateTo = p_fromdate 关
我正在尝试从分桶列中获取子集,然后获取交集。 这将从原始表中选择其他列。 我也对系列过滤持开放态度。 下面的代码报告 col1 不存在 - 不确定这是正确的方法。 WITH ranges AS (
SELECT friend_id FROM friendships WHERE user_id = 1; Returns: +-----------+ | friend_id | +---------
似乎无法在任何地方找到这个问题的答案。 我的游戏在用户触摸屏幕时开始,手指必须停留在一条路径内,如果它触摸/与边缘相交,那么我希望它运行 [self gameover] 方法。 边缘将是一个 UIIm
我有两个 RDD,一个非常大,另一个小得多。我想用小 RDD 的键在大 RDD 中找到所有唯一的元组。 大 RDD 太大,我必须避免完全洗牌 小型 RDD 也足够大,我无法广播它。我也许可以广播它的
所以我有两个函数的代码。第一个打印一个空的 20x20 板,第二个打印中间的一个字。现在我正在尝试编写一个函数来检查输入的单词是否会与同一字母的另一个单词(如填字游戏)相交。这是前两个函数的代码(此处
我正在一个网站上进行培训,该网站要求我制作一个程序,该程序将询问两个矩形的坐标并检查矩形是否相交。然后,当我发送程序时,网站会对其进行几次测试。它要求 A 矩形的 x 最小值、x 最大值、y 最小值和
我是一名优秀的程序员,十分优秀!