Java 以百分比表示的锚烷约束
我正在做一个项目,需要你的帮助。Java 以百分比表示的锚烷约束,java,javafx,javafx-2,javafx-8,Java,Javafx,Javafx 2,Javafx 8,我正在做一个项目,需要你的帮助。 我想知道,这是否可以在百分比中设置anchorpane约束 AnchorPane.setLeftAnchor(content1, 35%); 是的,可以通过在每次场景大小更改时更新约束值来实现: public class AnchorDemo extends Application { private final Button button = new Button("Add"); private final ListView list =
我想知道,这是否可以在百分比中设置anchorpane约束
AnchorPane.setLeftAnchor(content1, 35%);
是的,可以通过在每次场景大小更改时更新约束值来实现:
public class AnchorDemo extends Application {
private final Button button = new Button("Add");
private final ListView list = new ListView();
@Override
public void start(Stage primaryStage) {
AnchorPane root = new AnchorPane();
AnchorPane.setTopAnchor(list, 10.0);
AnchorPane.setLeftAnchor(list, 10.0);
AnchorPane.setTopAnchor(button, 10.0);
AnchorPane.setRightAnchor(button, 10.0);
root.getChildren().addAll(list, button);
Scene scene = new Scene(root, 300, 250);
scene.widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
updateWidthConstaints(newValue.doubleValue());
}
});
primaryStage.setScene(scene);
primaryStage.show();
updateWidthConstaints(scene.getWidth());
}
private void updateWidthConstaints(double width) {
// roughly give to the list 66% while to the button 33% of available
// space, besides paddings.
// +5s are for extra padding
AnchorPane.setRightAnchor(list, width * 1 / 3 + 5);
AnchorPane.setLeftAnchor(button, width * 2 / 3 + 5);
}
public static void main(String[] args) {
launch(args);
}
}
公共类AnchorDemo扩展应用程序{
专用最终按钮=新按钮(“添加”);
私有最终ListView列表=新建ListView();
@凌驾
公共无效开始(阶段primaryStage){
锚烷根=新锚烷();
AnchorPane.setTopAnchor(列表,10.0);
AnchorPane.setLeftAnchor(列表,10.0);
AnchorPane.setTopAnchor(按钮,10.0);
AnchorPane.setRightAnchor(按钮,10.0);
root.getChildren().addAll(列表,按钮);
场景=新场景(根,300,250);
scene.widthProperty().addListener(新的ChangeListener()){
@凌驾
public void已更改(ObservalEvalue是,可以通过在每次场景大小更改时更新约束值来更改:
public class AnchorDemo extends Application {
private final Button button = new Button("Add");
private final ListView list = new ListView();
@Override
public void start(Stage primaryStage) {
AnchorPane root = new AnchorPane();
AnchorPane.setTopAnchor(list, 10.0);
AnchorPane.setLeftAnchor(list, 10.0);
AnchorPane.setTopAnchor(button, 10.0);
AnchorPane.setRightAnchor(button, 10.0);
root.getChildren().addAll(list, button);
Scene scene = new Scene(root, 300, 250);
scene.widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
updateWidthConstaints(newValue.doubleValue());
}
});
primaryStage.setScene(scene);
primaryStage.show();
updateWidthConstaints(scene.getWidth());
}
private void updateWidthConstaints(double width) {
// roughly give to the list 66% while to the button 33% of available
// space, besides paddings.
// +5s are for extra padding
AnchorPane.setRightAnchor(list, width * 1 / 3 + 5);
AnchorPane.setLeftAnchor(button, width * 2 / 3 + 5);
}
public static void main(String[] args) {
launch(args);
}
}
公共类AnchorDemo扩展应用程序{
专用最终按钮=新按钮(“添加”);
私有最终ListView列表=新建ListView();
@凌驾
公共无效开始(阶段primaryStage){
锚烷根=新锚烷();
AnchorPane.setTopAnchor(列表,10.0);
AnchorPane.setLeftAnchor(列表,10.0);
AnchorPane.setTopAnchor(按钮,10.0);
AnchorPane.setRightAnchor(按钮,10.0);
root.getChildren().addAll(列表,按钮);
场景=新场景(根,300,250);
scene.widthProperty().addListener(新的ChangeListener()){
@凌驾
公共无效已更改(请评论并告知这是否可能请评论并告知这是否可能