javafx - JavaFX按钮背景向右溢出1px
问题描述
当我将新设置Background
为 aButton
时,Background
右侧多了 1px,颜色的不透明度 <1。
有没有办法删除 1px 或填充它(不透明度 1)?1px 并不多,但是如果你把它们放在一起,那 1px 就会变成一个可见的空白区域。当按钮添加到Pane
or时,我看到了问题Group
,当使用HBox
or时VBox
,1px 被填充。不幸的是,我需要使用Pane
or Group
。我尝试使用自定义插图,但问题仍然存在。
代码示例:
Background background = new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, Insets.EMPTY));
Font font = new Font("Times New Roman", 16);
Button a = new Button("A");
a.setFont(font);
a.setBackground(background);
a.setLayoutX(10);
a.setLayoutY(10);
pane.getChildren().add(a);
Button aa = new Button("AA");
aa.setFont(font);
aa.setBackground(background);
aa.setLayoutX(10);
aa.setLayoutY(45);
pane.getChildren().add(aa);
Button aaa = new Button("AAA");
aaa.setFont(font);
aaa.setBackground(background);
aaa.setLayoutX(10);
aaa.setLayoutY(80);
pane.getChildren().add(aaa);
结果(图像被放大,以便更好地看到 1px):
1px 的样子(红线上方的 1px):
1px 的样子
它看起来如何彼此相邻(您可以看到空间不是白色,也不是绿色,而是浅绿色:
彼此相邻的样子
解决方案
观察到的效果是按钮宽度不是整数的结果。这导致那些没有完全覆盖像素的部分被渲染为半透明。
Pane
不幸的是,没有考虑该snapToPixel
属性,只是将子项的大小调整为根据其属性计算的大小。
要解决这个问题,请使用一种Pane
将坐标捕捉到像素的类型,例如,VBox
或者在这种情况下,AnchorPane
没有设置任何锚点。
Pane pane = new AnchorPane();
推荐阅读
- c - 如何在不使用原始套接字发送 udp 请求后从端口读取 ICMP 响应
- linux - 如何在 Linux Mint / Ubuntu 上的 Visual Studios Code 中导航到页面末尾
- gradle - 如何获取 rootProject.name 并使其可用于应用程序、测试和分发?
- webpack - SaxonJS 无法与 webpack 一起使用
- amazon-dynamodb - 查询 dynamoDB 以获取 X 和 Y 之间的值
- python - 如何使用循环和范围功能在 python 中实现以下输出?
- python-3.x - 使用 numba 随机抽取类别的可能组合
- c# - 基于编译指令的 Xamarin 条件 xaml
- laravel - 尝试在存储桶文件夹中获取 S3 图像 URL
- java - 尝试调用虚拟方法:有一个错误 java.lang.NullPointerException