问题描述
我正在开发一个Java应用程序,需要在其中将代表日历事件的JComponent布局在一个容器中:
I am working on a java app in which I need to layout JComponents representing calendar events, in a container:
我使用自定义布局管理器,我怀疑是否是正确的解决方案.我根据事件的开始和结束时间计算顶部x位置和高度.我还计算每个事件的重叠事件数,从而计算出最大重叠数(代表最大列数).
I use custom layout manager, and I doubt if is the right solution. I calculate the top x position and the height, based on the event start and end time.I also calculate the number of overlapping events for each event, and as a result the max number of overlaps (which represents the max number of columns).
在计算每个组件的y位置和宽度时,由于存在许多重叠的可能性,因此变得很复杂.
When it comes to calculating y position and width of each component, it gets complicated because of many overlapping possibilities.
我的问题:是否有可用的布局管理器可以帮助我优化此类布局?还是应该继续使用和改进自定义布局管理器?
My question: is there an available layout manager which can help me optimize such layout ? or should I continue the using and improving the custom layout manager ?
推荐答案
在研究了各种布局管理器之后,对我来说很明显,需要完成的工作中最重要的部分是计算约束.
After exploring various layout managers, it became clear to me the most significant part of the work that needs to be done, is calculation the constrains.
我使用了以下方法:
对于代表一天的每个容器:
For each container representing a day :
- 根据时标计算每个JComponent的Y位置(垂直)和高度(代表单个日历事件).
- 要计算每个JComponent的X位置和宽度,我必须分析重叠的组件,并找出每个子列"(或子列 s )应该占据的位置.
- 遍历所有容器的JComponent(按高度排序,代表持续时间),针对所有其他JComponent进行时间重叠的JComponent检查.这样做,我能够计算出每个JComponent所需的子列数和相应的子列(X位置).
- 基于每个JComponent的X位置,我能够通过搜索下一个重叠的JComponent(如果有)来计算其宽度.下一个重叠的JComponent的X位置限制了宽度.
在具有所有需要的约束之后,我可以使用简单的布局管理器(如Formlayout),但是我看不到这样做的价值.为每个JComponent设置界限很简单.
After having all the needed constrains, I could use a simply layout manager (like Formlayout), but I did not see the value of doing so. Setting bounds for each JComponent was straight forward.
这篇关于表示日历事件的摆动JComponent的布局管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!