Javafx数字四舍五入
我正在开发我的第一个JavaFX应用程序,我遇到了一个数字绑定问题。我有一个数字:Javafx数字四舍五入,javafx,binding,rounding,Javafx,Binding,Rounding,我正在开发我的第一个JavaFX应用程序,我遇到了一个数字绑定问题。我有一个数字: NumberBinding cellSize = Bindings.min(scene.widthProperty().subtract(150).divide(nbC.getValue()), scene.heightProperty().subtract(150).divide(nbR.getValue())); 然后,我将绑定到此单元格大小的widthProperty()和heightProperty()
NumberBinding cellSize = Bindings.min(scene.widthProperty().subtract(150).divide(nbC.getValue()), scene.heightProperty().subtract(150).divide(nbR.getValue()));
然后,我将绑定到此单元格大小的widthProperty()
和heightProperty()
窗格放入我的网格窗格gPane
我想要的是有一个与网格窗格
宽度和高度相同的标签,这样我就把它们都放在一个堆叠窗格中,这样标签就在网格窗格
上,完全覆盖了它
我尝试将标签prefWidthProperty()
和prefHeightProperty()
绑定到gPane
的宽度和高度属性,但它不起作用,因为它的实际宽度和高度似乎不是列数和行数乘以cellSize
所以我现在使用的代码是:
winLabel = new Label("Bravo vous avez gagné ! :)");
winLabel.prefWidthProperty().bind(cellSize.multiply(nbC.getValue()));
winLabel.prefHeightProperty().bind(cellSize.multiply(nbR.getValue()));
它工作得很好,除了标签比我的窗格小一点,有时窗格的大小一次只跳几个像素,在这种情况下,标签的大小对应于窗格的大小。
我想这是因为手机的大小被四舍五入(所以当我们接近下一个胭脂值时,手机的大小会增加)
我用DoubleBinding
尝试了2个小时,尝试了所有的方法,但似乎没有取得任何进展。你能帮我吗
这是一个mvce:
public class Mvce extends Application {
private Scene scene;
// will be the root of our scene
private AnchorPane anchor;
private GridPane gPane;
private Label winLabel;
// this stackPane will contain the grid and the winLabel
private StackPane stack;
// number of columns and rows, from which we calculate the binding cellSize
private IntegerProperty nbC;
private IntegerProperty nbR;
private NumberBinding cellSize;
private static final int WINDOW_SIZE=720;
public void addWinLabel(){
DoubleProperty fontSize = new SimpleDoubleProperty();
fontSize.bind(cellSize.multiply(nbC.getValue()).divide(15));
winLabel = new Label("Congratulations, you won ! :)");
winLabel.prefWidthProperty().bind(cellSize.multiply(nbC.getValue()));
winLabel.prefHeightProperty().bind(cellSize.multiply(nbR.getValue()));
winLabel.setTextFill(Color.web("#3099AA"));
winLabel.setAlignment(Pos.CENTER);
winLabel.styleProperty().bind(Bindings.concat("-fx-font-size: ", fontSize.asString(), ";",
"-fx-background-color: rgba(200, 200, 200, 0.85);"));
anchor.getChildren().add(winLabel);
anchor.setTopAnchor(winLabel, 75.0);
anchor.setLeftAnchor(winLabel, 75.0);
}
public void createPanes(){
anchor.getChildren().remove(stack);
stack = new StackPane();
gPane = new GridPane();
cellSize = Bindings.min(scene.widthProperty().subtract(150).divide(nbC.getValue()), scene.heightProperty().subtract(150).divide(nbR.getValue()));
for(int c=0; c<nbC.getValue(); c++){
for(int r=0; r<nbR.getValue(); r++){
if(c==0){
RowConstraints row = new RowConstraints();
row.setPercentHeight(100/nbR.getValue());
gPane.getRowConstraints().add(row);
}
Pane pane = new StackPane();
pane.minWidthProperty().bind(cellSize);
pane.minHeightProperty().bind(cellSize);
pane.maxWidthProperty().bind(cellSize);
pane.maxHeightProperty().bind(cellSize);
pane.setStyle("-fx-background-color: #FFFFFF");
gPane.add(pane,c,r);
}
}
gPane.setGridLinesVisible(true);
stack.getChildren().addAll(gPane);
anchor.getChildren().add(stack);
anchor.setTopAnchor(stack, 75.0);
anchor.setLeftAnchor(stack, 75.0);
addWinLabel();
}
@Override
public void start(Stage primaryStage) {
primaryStage.setMinWidth(720.0);
primaryStage.setMinHeight(720.0);
// main panes
anchor = new AnchorPane();
scene = new Scene(anchor, WINDOW_SIZE, WINDOW_SIZE, Color.LIGHTBLUE);
nbC = new SimpleIntegerProperty(7);
nbR = new SimpleIntegerProperty(7);
createPanes();
primaryStage.setTitle("mvce");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
公共类Mvce扩展应用程序{
私密场景;
//将是我们场景的根源
私人锚;
专用网格窗格gPane;
私有标签;
//此stackPane将包含网格和winLabel
私有堆栈;
//列数和行数,从中计算绑定单元格大小
私有集成财产nbC;
私有集成财产nbR;
专用号码分机;
私有静态最终整数窗口大小=720;
public void addWinLabel(){
DoubleProperty fontSize=新的SimpleDoubleProperty();
bind(cellSize.multiply(nbC.getValue()).divide(15));
winLabel=新标签(“祝贺你,你赢了!:)”;
winLabel.prefWidthProperty().bind(cellSize.multiply(nbC.getValue());
winLabel.prefHeightProperty().bind(cellSize.multiply(nbR.getValue());
winLabel.setTextFill(Color.web(“#3099AA”);
winLabel.setAlignment(位置中心);
winLabel.styleProperty().bind(Bindings.concat(“-fx font-size:”,fontSize.asString(),“;”,
“-fx背景色:rgba(2002002002000.85);”;
anchor.getChildren().add(winLabel);
anchor.setTopAnchor(winLabel,75.0);
setLeftAnchor(winLabel,75.0);
}
公共void createPanes(){
anchor.getChildren().remove(堆栈);
stack=新的StackPane();
gPane=新网格窗格();
cellSize=Bindings.min(scene.widthProperty().subtract(150).divide(nbC.getValue()),scene.heightProperty().subtract(150).divide(nbR.getValue());
对于(int c=0;c我终于找到了一个解决方案。您必须删除行约束
,然后添加标签
,并将其与其父项堆栈窗格
绑定。这是我修改的mcve
public class Mvce extends Application {
private Scene scene;
// will be the root of our scene
private AnchorPane anchor;
private GridPane gPane;
private Label winLabel;
// this stackPane will contain the grid and the winLabel
private StackPane stack;
// number of columns and rows, from which we calculate the binding cellSize
private IntegerProperty nbC;
private IntegerProperty nbR;
private NumberBinding cellSize;
private static final int WINDOW_SIZE = 720;
public void addWinLabel() {
DoubleProperty fontSize = new SimpleDoubleProperty();
fontSize.bind(cellSize.multiply(nbC.getValue()).divide(15));
winLabel = new Label("Congratulations, you won ! :)");
winLabel.prefWidthProperty().bind(stack.widthProperty());
winLabel.prefHeightProperty().bind(stack.heightProperty());
winLabel.setTextFill(Color.web("#3099AA"));
winLabel.setAlignment(Pos.CENTER);
winLabel.styleProperty().bind(Bindings.concat("-fx-font-size: ", fontSize.asString(), ";",
"-fx-background-color: rgba(200, 200, 200, 0.85);"));
anchor.getChildren().add(winLabel);
AnchorPane.setTopAnchor(winLabel, 75.0);
AnchorPane.setLeftAnchor(winLabel, 75.0);
}
public void createPanes() {
anchor.getChildren().remove(stack);
stack = new StackPane();
gPane = new GridPane();
cellSize = Bindings.min(scene.widthProperty().subtract(150).divide(nbC.getValue()),
scene.heightProperty().subtract(150).divide(nbR.getValue()));
for(int c = 0; c < nbC.getValue(); c++) {
for(int r = 0; r < nbR.getValue(); r++) {
// Comment these lines, as you set the size thanks to panes you add.
// if(c == 0) {
// RowConstraints row = new RowConstraints();
// row.setPercentHeight(100 / nbR.getValue());
// gPane.getRowConstraints().add(row);
// }
Pane pane = new StackPane();
// Weird to me to bind min and max width and height, prefer constant value and prefHeight / prefWidth binding with the cellSize
pane.minWidthProperty().bind(cellSize);
pane.minHeightProperty().bind(cellSize);
pane.maxWidthProperty().bind(cellSize);
pane.maxHeightProperty().bind(cellSize);
pane.setStyle("-fx-background-color: #FFFFFF");
gPane.add(pane, c, r);
}
}
gPane.setGridLinesVisible(true);
stack.getChildren().addAll(gPane);
anchor.getChildren().add(stack);
AnchorPane.setTopAnchor(stack, 75.0);
AnchorPane.setLeftAnchor(stack, 75.0);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setMinWidth(720.0);
primaryStage.setMinHeight(720.0);
// main panes
anchor = new AnchorPane();
scene = new Scene(anchor, WINDOW_SIZE, WINDOW_SIZE, Color.LIGHTBLUE);
nbC = new SimpleIntegerProperty(7);
nbR = new SimpleIntegerProperty(7);
createPanes();
addWinLabel();
primaryStage.setTitle("mvce");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
公共类Mvce扩展应用程序{
私密场景;
//将是我们场景的根源
私人锚;
专用网格窗格gPane;
私有标签;
//此stackPane将包含网格和winLabel
私有堆栈;
//列数和行数,从中计算绑定单元格大小
私有集成财产nbC;
私有集成财产nbR;
专用号码分机;
私有静态最终整数窗口大小=720;
public void addWinLabel(){
DoubleProperty fontSize=新的SimpleDoubleProperty();
bind(cellSize.multiply(nbC.getValue()).divide(15));
winLabel=新标签(“祝贺你,你赢了!:)”;
winLabel.prefWidthProperty().bind(stack.widthProperty());
winLabel.prefHeightProperty().bind(stack.heightProperty());
winLabel.setTextFill(Color.web(“#3099AA”);
winLabel.setAlignment(位置中心);
winLabel.styleProperty().bind(Bindings.concat(“-fx font-size:”,fontSize.asString(),“;”,
“-fx背景色:rgba(2002002002000.85);”;
anchor.getChildren().add(winLabel);
AnchorPane.setTopAnchor(winLabel,75.0);
AnchorPane.setLeftAnchor(winLabel,75.0);
}
公共void createPanes(){
anchor.getChildren().remove(堆栈);
stack=新的StackPane();
gPane=新网格窗格();
cellSize=Bindings.min(scene.widthProperty().subtract(150).divide(nbC.getValue()),
scene.heightProperty().subtract(150).divide(nbR.getValue());
对于(int c=0;c