更新标签以显示JavaFX场景的宽度和高度。

huangapple go评论51阅读模式
英文:

Updating Labels to show the width and height of the scene in JavaFX

问题

我正在尝试创建一个小的JavaFX程序,该程序会打印出当前窗口的宽度和高度,并使用属性和绑定自动更新它。

我尝试了很多方法,最终得出了这个解决方案:

import javafx.application.Application;
import javafx.beans.property.*;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class App extends Application {

    @Override
    public void start(Stage stage) {
        VBox root = new VBox();
        Label width = new Label();
        Label height = new Label();
        TextField widthTf = new TextField();
        TextField heightTf = new TextField();
        Button button = new Button("Resize");
        root.getChildren().addAll(width, height, widthTf, heightTf, button);

        IntegerProperty widthInteger = new SimpleIntegerProperty(320);
        IntegerProperty heightInteger = new SimpleIntegerProperty(240);

        Scene scene = new Scene(root, widthInteger.getValue(), heightInteger.getValue());

        width.textProperty().bind(widthInteger.asString());
        height.textProperty().bind(heightInteger.asString());

        stage.setScene(scene);
        button.setOnAction(e -> {
            widthInteger.setValue(Integer.parseInt(widthTf.getText()));
            heightInteger.setValue(Integer.parseInt(heightTf.getText()));
            stage.setWidth(widthInteger.getValue());
            stage.setHeight(heightInteger.getValue());
            widthTf.clear();
            heightTf.clear();
        });

        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这看起来可能不太美观,但这并不是我的主要目标,虽然使用按钮确实会更新值并调整窗口大小,但对我来说感觉像是作弊。我相信肯定有比这更好的方法,如果有的话,我想知道确切的方法。我尝试使用scene.getWidth()scene.getHeight(),但是无法解决问题。我可能只是想得太多,但这确实感觉像是一个欺骗性的解决方案,我觉得应该有更好、更清晰的方法。

提前感谢你的帮助。

英文:

I'm trying to make a small JavaFX program that prints out the width and height of the current window, and updates it automatically using properties and bindings.

I tried a lot of things, and came to this solution:

import javafx.application.Application;
import javafx.beans.property.*;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage stage) {
VBox root = new VBox();
Label width = new Label();
Label height = new Label();
TextField widthTf = new TextField();
TextField heightTf = new TextField();
Button button = new Button("Resize");
root.getChildren().addAll(width, height, widthTf, heightTf, button);
IntegerProperty widthInteger = new SimpleIntegerProperty(320);
IntegerProperty heightInteger = new SimpleIntegerProperty(240);
Scene scene = new Scene(root, widthInteger.getValue(), heightInteger.getValue());
width.textProperty().bind(widthInteger.asString());
height.textProperty().bind(heightInteger.asString());
stage.setScene(scene);
button.setOnAction(e -> {
widthInteger.setValue(Integer.parseInt(widthTf.getText()));
heightInteger.setValue(Integer.parseInt(heightTf.getText()));
stage.setWidth(widthInteger.getValue());
stage.setHeight(heightInteger.getValue());
widthTf.clear();
heightTf.clear();
});
stage.show();
}
public static void main(String[] args) { launch(args);}
}

It is definitely not pleasing to the eyes, but that is not my main goal here, and even tho using the button does update the values and resize the window, it just feels like cheating to me. I'm pretty sure there has to be a better way than this, and if there is, I would like to know what exactly. I tried using scene.getWidth() and scene.getHeight(), but I couldn't solve it with them.
I might be just overthinking this, but it definitely feels like a cheating solution and like there is a better and cleaner way.

Thanks for the help in advance.

答案1

得分: 2

如果目标仅仅是显示窗口的宽度和高度标签,Stage 提供了 widthProperty()heightProperty()。你可以直接将它们绑定到标签上:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class App extends Application {

    @Override
    public void start(Stage stage) {
        VBox root = new VBox();
        Label width = new Label();
        Label height = new Label();
        root.getChildren().addAll(width, height);

        Scene scene = new Scene(root, 320, 240);

        width.textProperty().bind(stage.widthProperty().asString());
        height.textProperty().bind(stage.heightProperty().asString());

        stage.setScene(scene);

        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

如果你希望它们显示为整数,你可以提供一个格式化字符串:

width.textProperty().bind(stage.widthProperty().asString("%.0f"));

等等。

英文:

If the goal is simply to have labels which display the width and height of the window, Stage exposes a widthProperty() and a heightProperty(). You can just bind directly to those:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage stage) {
VBox root = new VBox();
Label width = new Label();
Label height = new Label();
root.getChildren().addAll(width, height);
Scene scene = new Scene(root, 320, 240);
width.textProperty().bind(stage.widthProperty().asString());
height.textProperty().bind(stage.heightProperty().asString());
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) { launch(args);}
}

If you want them as integers, you can provide a format string:

width.textProperty().bind(stage.widthProperty().asString("%.0f"));

etc.

huangapple
  • 本文由 发表于 2023年3月15日 18:53:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定