这是具有不同填充的4个按钮的示例,它演示了填充值的舍入。
第一个按钮:(3, 6,4,6) 第二个按钮:(2.5,6,4.5,6) 第三个按钮:(2,6,5,6) 第四个按钮:(2.4,6,4.6,6)

我希望使用某种抗锯齿技术来创建文本被放置在像素之间的错觉。
这种舍入可以避免吗?如果不是,那么填充双精度值的目的是什么?
public class ButtonTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
StackPane root = new StackPane();
HBox hBox = new HBox();
hBox.setAlignment(Pos.CENTER);
Button firstButton = new Button("Text");
Button secondButton = new Button("Text");
Button thirdButton = new Button("Text");
Button fourthButton = new Button("Text");
Insets firstPadding = new Insets(3, 6, 4, 6);
Insets secondPadding = new Insets(2.5, 6, 4.5, 6);
Insets thirdPadding = new Insets(2, 6, 5, 6);
Insets fourthPadding = new Insets(2.4, 6, 4.6, 6);
firstButton.setPadding(firstPadding);
secondButton.setPadding(secondPadding);
thirdButton.setPadding(thirdPadding);
fourthButton.setPadding(fourthPadding);
firstButton.setTooltip(new Tooltip(firstPadding.toString()));
secondButton.setTooltip(new Tooltip(secondPadding.toString()));
thirdButton.setTooltip(new Tooltip(thirdPadding.toString()));
fourthButton.setTooltip(new Tooltip(fourthPadding.toString()));
hBox.getChildren().add(firstButton);
hBox.getChildren().add(secondButton);
hBox.getChildren().add(thirdButton);
hBox.getChildren().add(fourthButton);
root.getChildren().add(hBox);
Scene scene = new Scene(root, 300, 200);
scene.getStylesheets().add("test.css");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}CSS:
.button {
-fx-snap-to-pixel: false;
}更新
奇怪的是:如果按钮沿Y轴平移0.5或-0.5,那么按钮会移动,但文本保持不变。
第一个按钮不平移,第二个按钮平移-0.5,第三个按钮平移-1。都有相同的填充物。

第二个按钮像预期的那样模糊,因为它位于中间像素位置,但由于某种原因,文本似乎只能通过整数值进行转换。也许文本被限制为整数翻译,以确保它始终是清晰(可读)的,并且不会过于模糊。
发布于 2015-05-26 10:21:34
虽然Inset()的参数是双精度的,但没有这样的东西,这只是为了方便。例如,您可能在循环中和/或通过数学运算到达这些坐标,JavaFX将您从自己进行舍入操作中解放出来。最终,坐标必须映射到像素-地址当然是整数,因此需要四舍五入。如果您的屏幕分辨率为1400x1050,则这些像素仅为您协调过的像素。
至于抗锯齿,您可以像在Scene(Parent root, double width, double height, boolean depthBuffer, SceneAntialiasing antiAliasing).中一样在定义scene时进行设置
https://stackoverflow.com/questions/30447692
复制相似问题