javafx有布局管理器吗

最近,我不得不为应用程序实现一个布局,其中可以根据用户是否登录来隐藏或显示菜单区和状态区,并通过滑入/滑出动画显示该区域。 以下视频显示了实际的布局:

过去,我可能会使用自定义控件和自定义布局代码来实现这种行为(例如“在外观中覆盖layoutChildren()方法”)。 但是这次我的设置有所不同,因为我使用的是Adam Bien的afterburner.fx ,现在我有了FXML和一个控制器类。

那该怎么办呢? 我决定尝试使用锚定窗格来实现运气,并通过时间轴实例更新堆栈窗格上的约束。 约束存储在堆栈窗格的可观察属性图中。 只要这些限制发生变化,就会自动请求锚定窗格的布局。 如果发生这种情况而没有任何闪烁,那么我们最终会得到一个很好的平滑动画。 顺便说一句,来自Swing,我总是希望闪烁,但是JavaFX通常不会发生闪烁。

最后,我写了下面的控制器类来管理锚窗格及其子堆栈窗格。 请注意中间属性menuPaneLocationbottomPaneLocation的小技巧。 它们是必需的,因为动画时间轴可与属性一起使用。 因此,它会更新这些属性,并且每当它们更改时,都会应用新的锚定窗格约束。

import static javafx.scene.layout.AnchorPane.setBottomAnchor;
import static javafx.scene.layout.AnchorPane.setLeftAnchor;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.fxml.FXML;
import javafx.scene.layout.StackPane;
import javafx.util.Duration;</code>/*** This presenter covers the top-level layout concepts of the workbench.*/
public class WorkbenchPresenter {@FXML
private StackPane topPane;@FXML
private StackPane menuPane;@FXML
private StackPane centerPane;@FXML
private StackPane bottomPane;public WorkbenchPresenter() {
}private final BooleanProperty showMenuPane = new SimpleBooleanProperty(this, "showMenuPane", true);public final boolean isShowMenuPane() {return showMenuPane.get();
}public final void setShowMenuPane(boolean showMenu) {showMenuPane.set(showMenu);
}/**
* Returns the property used to control the visibility of the menu panel.
* When the value of this property changes to false then the menu panel will
* slide out to the left).
*
* @return the property used to control the menu panel
*/
public final BooleanProperty showMenuPaneProperty() {return showMenuPane;
}private final BooleanProperty showBottomPane = new SimpleBooleanProperty(this, "showBottomPane", true);public final boolean isShowBottomPane() {return showBottomPane.get();
}public final void setShowBottomPane(boolean showBottom) {showBottomPane.set(showBottom);
}/**
* Returns the property used to control the visibility of the bottom panel.
* When the value of this property changes to false then the bottom panel
* will slide out to the left).
*
* @return the property used to control the bottom panel
*/
public final BooleanProperty showBottomPaneProperty() {return showBottomPane;
}public final void initialize() {menuPaneLocation.addListener(it -> updateMenuPaneAnchors());bottomPaneLocation.addListener(it -> updateBottomPaneAnchors());showMenuPaneProperty().addListener(it -> animateMenuPane());showBottomPaneProperty().addListener(it -> animateBottomPane());menuPane.setOnMouseClicked(evt -> setShowMenuPane(false));centerPane.setOnMouseClicked(evt -> {setShowMenuPane(true);setShowBottomPane(true);});bottomPane.setOnMouseClicked(evt -> setShowBottomPane(false));
}/** The updateMenu/BottomPaneAnchors methods get called whenever the value of* menuPaneLocation or bottomPaneLocation changes. Setting anchor pane* constraints will automatically trigger a relayout of the anchor pane* children.*/private void updateMenuPaneAnchors() {setLeftAnchor(menuPane, getMenuPaneLocation());setLeftAnchor(centerPane, getMenuPaneLocation() + menuPane.getWidth());
}private void updateBottomPaneAnchors() {setBottomAnchor(bottomPane, getBottomPaneLocation());setBottomAnchor(centerPane, getBottomPaneLocation() + bottomPane.getHeight());setBottomAnchor(menuPane,getBottomPaneLocation() + bottomPane.getHeight());
}/*
* Starts the animation for the menu pane.
*/
private void animateMenuPane() {if (isShowMenuPane()) {slideMenuPane(0);} else {slideMenuPane(-menuPane.prefWidth(-1));}
}/*
* Starts the animation for the bottom pane.
*/
private void animateBottomPane() {if (isShowBottomPane()) {slideBottomPane(0);} else {slideBottomPane(-bottomPane.prefHeight(-1));}
}/** The animations are using the JavaFX timeline concept. The timeline updates* properties. In this case we have to introduce our own properties further* below (menuPaneLocation, bottomPaneLocation) because ultimately we need to* update layout constraints, which are not properties. So this is a little* work-around.*/private void slideMenuPane(double toX) {KeyValue keyValue = new KeyValue(menuPaneLocation, toX);KeyFrame keyFrame = new KeyFrame(Duration.millis(300), keyValue);Timeline timeline = new Timeline(keyFrame);timeline.play();
}private void slideBottomPane(double toY) {KeyValue keyValue = new KeyValue(bottomPaneLocation, toY);KeyFrame keyFrame = new KeyFrame(Duration.millis(300), keyValue);Timeline timeline = new Timeline(keyFrame);timeline.play();
}private DoubleProperty menuPaneLocation = new SimpleDoubleProperty(this, "menuPaneLocation");private double getMenuPaneLocation() {return menuPaneLocation.get();
}private DoubleProperty bottomPaneLocation = new SimpleDoubleProperty(this, "bottomPaneLocation");private double getBottomPaneLocation() {return bottomPaneLocation.get();
}
}

以下是此项工作所需的FXML:

<?xml version="1.0" encoding="UTF-8"?><?import java.lang.*?>
<?import javafx.scene.layout.*?><AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.workbench.WorkbenchPresenter"><children><StackPane fx:id="bottomPane" layoutX="-4.0" layoutY="356.0" prefHeight="40.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" /><StackPane fx:id="menuPane" layoutY="28.0" prefWidth="200.0" AnchorPane.bottomAnchor="40.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="40.0" /><StackPane fx:id="topPane" prefHeight="40.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /><StackPane fx:id="centerPane" layoutX="72.0" layoutY="44.0" AnchorPane.bottomAnchor="40.0" AnchorPane.leftAnchor="200.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="40.0" /></children>
</AnchorPane>

翻译自: https://www.javacodegeeks.com/2015/02/javafx-tip-17-animated-workbench-layout-anchorpane.html

javafx有布局管理器吗

javafx有布局管理器吗_JavaFX技巧17:带有AnchorPane的动画工作台布局相关推荐

  1. JavaFX技巧17:带有AnchorPane的动画工作台布局

    最近,我不得不为应用程序实现一个布局,其中可以根据用户是否登录来隐藏或通过滑入/滑出动画显示或显示菜单区域和状态区域. 以下视频显示了实际的布局: 在过去,我可能会使用自定义控件和自定义布局代码来实现 ...

  2. 窗口管理器 实现_「42」Python布局管理器(三):place实现组件的精确与灵活布局...

    已经学习了两类布局管理器: Pack布局管理器:按照垂直或者水平的方向自然排布: Grid布局管理器:采用表格结构组织组件,组件位置受限表格形式. 两类管理器都属于那种很古板的布局方式,不能适应需要相 ...

  3. JAVA布局管理器导包_在 Java 中,要使用布局管理器,必须导入下列( )包。_计算机网络基础答案_学小易找答案...

    [单选题]详图索引符号中的分子编号4代表( ). [单选题]MCS - 51 单片机的 CPU 主要的组成部分为( ) [判断题]路由器工作在物理层,其实现路径选择和寻址的功能.( ); [单选题]禅 ...

  4. 【Android 应用开发】AndroidUI设计之 布局管理器 - 详细解析布局实现

    写完博客的总结 : 以前没有弄清楚的概念清晰化 父容器与本容器属性 : android_layout...属性是本容器的属性, 定义在这个布局管理器的LayoutParams内部类中, 每个布局管理器 ...

  5. 3、Swing布局管理器

    在使用 Swing 向容器添加组件时,需要考虑组件的位置和大小.如果不使用布局管理器,则需要先在纸上画好各个组件的位置并计算组件间的距离,再向容器中添加.这样虽然能够灵活控制组件的位置,实现却非常麻烦 ...

  6. Java可视化编程,基于布局管理器的UI设计

    在<事件驱动模型>讲述了如何将用户与功能实现代码联系到一起.怎么样便于用户理解和符合用户的使用习惯? 本篇还是就此问题作分析,站在用户角度上分析UI各组件倒底该如何设计呈现. 优秀的UI会 ...

  7. re管理器Java_自定义布局管理器-FormLayout

    第二部分:自定义布局管理器 在java.awt包与javax.swing包下有许多现成的布局类,比如BorderLayout.FlowLayout,还有较为复杂的.用于精确定位的布局类GridBagL ...

  8. java gui 布局 旋转_JAVA GUI编程之布局管理器

    JAVA的GUI(图形用户界面)由各种组件构成,主要分为AWT组件(java.awt)以及功能更强的Swing组件(javax.swing)两种. 组件可以分为容器组件和非容器组件.容器组件是指可以包 ...

  9. 约束布局管理器 CAConstraintLayoutManager 以及其不起作用

    当使用 CABasicAnimation时,CAConstraintLayoutManager不起作用,原因未知 CoreAnimation编程指南(九)图层布局 发布日期:2012-11-21 浏览 ...

最新文章

  1. 十个利用矩阵乘法解决的经典题目
  2. Swift_错误处理
  3. No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer解决方法
  4. GNU/Linux下LVM配置管理以及快照卷、物理卷、卷组、逻辑卷的创建和删除
  5. Redis Cluster搭建方法简介22211111
  6. gridview添加header
  7. P5748 集合划分计数(贝尔数/多项式)
  8. 灰色关联与TOPSIS法 —— matlab
  9. Churchman University简介:
  10. adt变频器故障代码ol2_误诊实例换来的变频器维修经验
  11. 20145307《信息安全系统设计基础》第二周学习总结
  12. 打造SpringBootTemplate(SpringBoot项目的模版)
  13. Uber 开源深度学习分布训练库 Petastorm
  14. sshd启动报错解决:sshd re-exec requires execution with an absolute path
  15. [老老实实学WCF] 第五篇 再探通信--ClientBase
  16. 2021年茶艺师(初级)考试及茶艺师(初级)新版试题
  17. 想加薪怎么和领导谈?学会这四招轻松涨薪
  18. 6岁女孩出口之乎者也 用《论语》典故批评妈妈
  19. android 微信跨境支付,微信跨境支付已在超过49个境外国家和地区合规接入
  20. 运动耳机什么牌子好、这五款是最值得推荐的运动耳机

热门文章

  1. YbtOJ#763-攻城略池【线段树合并】
  2. 【2018.3.31】模拟赛之二-ssl2407 负进制【贪心】
  3. 【区间DP】甲虫(luogu 4870)
  4. Vs Code如何自定义设置一键代码补全
  5. 红歌合唱之团结就是力量
  6. 《四世同堂》金句摘抄(七)
  7. 人脸认证源码faceIdentify
  8. struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Dept实体类和对应的配置信息
  9. Sring类型数组赋值
  10. android之视频直播与播放Vitamio