当舞台大小发生变化时,如何使窗格和元素在其中可调整大小

时间:2017-12-05 19:39:22

标签: javafx

请告诉我,如何在使用mousedragg调整舞台大小的同时调整窗格的整个内容。这是我的代码:

public class CustomElement<T>
{
    [XmlText]
    public T Value;

    public CustomElement()
    {
    }

    public CustomElement(T value)
    {
        Value = value;
    }

    [XmlIgnore]
    public bool? Something { get; set; }

    [XmlAttribute("something")]
    public bool XmlSomething
    {
        get => Something != null && Something.Value;
        set => Something = value;
    }

    public bool ShouldSerializeXmlSomething() => Something.HasValue;

    public static implicit operator CustomElement<T>(T x)
    {
        return new CustomElement<T>(x);
    }
}

[XmlRoot("item")]
[XmlType(TypeName = "ci")]
public class Item
{
    [XmlElement("attribute1")]
    public CustomElement<string> Attribute1 { get; set; }

    [XmlElement("attribute2")]
    public CustomElement<string> Attribute2 { get; set; }

    [XmlElement("attribute3")]
    public CustomElement<string> Attribute3 { get; set; }

    // Etc Etc
}

我认为有绑定的想法。但是我不知道如何使用它,以便在舞台大小发生变化时使窗格的所有节点都可以调整大小。

我正在搜索没有Fxml或sceneBuilder的解决方案。

提前谢谢。

2 个答案:

答案 0 :(得分:2)

如果您坚持使用Pane容器,请在行scene = new Scene(pane, 640, 640);之后添加:

scene.widthProperty().addListener((c,o,n)->button.setPrefWidth((Double)n));
scene.heightProperty().addListener((c,o,n)->button.setPrefHeight((Double)n));

并在stage.setScene(scene);行后添加:

button.setPrefSize(scene.getWidth(), scene.getHeight());

这适用于Pane并执行您需要的操作。

但我更喜欢使用AnchorPane容器并将Top,Right,Bottom和Left锚点设置为0.

答案 1 :(得分:1)

如果您希望将按钮的宽度绑定到场景宽度

,这是解决方案
button.minWidthProperty().bind(scene.widthProperty());

你也可以修改这个+/-你想要的任何东西

button.minWidthProperty().bind(scene.widthProperty().subtract(20));

你可以为身高做同样的事情

button.minHeightProperty().bind(scene.heightProperty().subtract(200));
相关问题