JavaFX中Slider thumb的坐标 [英] Coordinates of Slider thumb in JavaFX
本文介绍了JavaFX中Slider thumb的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一些方法可以在 JavaFX
中知道Slider拇指的位置?
解决方案
使用
import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.control。*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
公共类SlipSlide扩展Application {
@Override
public void start(舞台阶段){
Slider slider = new Slider(1,10,3);
slider.setMajorTickUnit(9);
slider.setMinorTickCount(8);
slider.setSnapToTicks(true);
slider.setShowTickMarks(true);
slider.setShowTickLabels(true);
slider.setPrefWidth(250);
Label boundsLabel = new Label();
VBox layout = new VBox(10,slider,boundsLabel);
stage.setScene(new Scene(layout));
stage.show();
reportThumbBounds(slider,boundsLabel);
slider.valueProperty()。addListener((observable,oldValue,newValue) - > {
reportThumbBounds(slider,boundsLabel);
});
slider.boundsInParentProperty()。addListener((observable,oldValue,newValue) - > {
reportThumbBounds(slider,boundsLabel);
});
}
private void reportThumbBounds(Slider slider,Label boundsLabel){
Bounds bounds = slider.lookup(。thumb)。getBoundsInParent();
boundsLabel.setText(
String.format(
(%。2f,%。2f)(%。2f,%。2f),
bounds.getMinX() ,bounds.getMinY(),
bounds.getMaxX(),bounds.getMaxY()
)
);
}
public static void main(String [] args){
launch(args);
}
}
There is some method to know the position of a Slider thumb in JavaFX
?
解决方案
Use getBoundsInParent()
and a lookup()
:
Bounds bounds = slider.lookup(".thumb").getBoundsInParent();
You may need to applyCss()
to the slider before the lookup in order for it to work.
import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SlipSlide extends Application {
@Override
public void start(Stage stage) {
Slider slider = new Slider(1, 10, 3);
slider.setMajorTickUnit(9);
slider.setMinorTickCount(8);
slider.setSnapToTicks(true);
slider.setShowTickMarks(true);
slider.setShowTickLabels(true);
slider.setPrefWidth(250);
Label boundsLabel = new Label();
VBox layout = new VBox(10, slider, boundsLabel);
stage.setScene(new Scene(layout));
stage.show();
reportThumbBounds(slider, boundsLabel);
slider.valueProperty().addListener((observable, oldValue, newValue) -> {
reportThumbBounds(slider, boundsLabel);
});
slider.boundsInParentProperty().addListener((observable, oldValue, newValue) -> {
reportThumbBounds(slider, boundsLabel);
});
}
private void reportThumbBounds(Slider slider, Label boundsLabel) {
Bounds bounds = slider.lookup(".thumb").getBoundsInParent();
boundsLabel.setText(
String.format(
"(%.2f,%.2f) (%.2f,%.2f)",
bounds.getMinX(), bounds.getMinY(),
bounds.getMaxX(), bounds.getMaxY()
)
);
}
public static void main(String[] args) {
launch(args);
}
}
这篇关于JavaFX中Slider thumb的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文