tl;博士:我想做第二张图片中的内容(忽略红线)
我了解 GroupLayout 的工作原理,但我无法弄清楚,或者它是否可能。我最初有这个代码:
#Horizontal layout is a parallel group containing 3 sequences of components
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) #everything else to the right
.addGroup(layout.createSequentialGroup() #row 1
.addComponent(startTimeLabel)
.addComponent(self.lastUpdatedLabel))
.addGroup(layout.createSequentialGroup() #row 2
.addComponent(self.progressBar)
.addComponent(self.clearBtn))
.addGroup(layout.createSequentialGroup() #row 3
.addComponent(self.fileProgLabel)
.addComponent(self.sizeProgLabel)
.addComponent(self.ETCLabel))))
产生了这个:
但是,我想对齐进度条开始和结束处的 2 个顶部标签,以及进度条开始、中间和结束处的 3 个底部标签,如下所示(mspainted):
我的第一个方法是尝试将组件拆分为我用上面的行创建的并行组。我将支柱放在进度条的两侧,将 4 个末端标签与那些对齐,并将中心标签与进度条本身对齐:
#Horizontal layout is a sequence of 3 parallel groups of components, and an end component
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) #starttime, strut, filesprog
.addComponent(startTimeLabel)
.addComponent(progLeft) #invisible, just for gluing things to
.addComponent(self.fileProgLabel))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER) #progress bar, sizeprog
.addComponent(self.progressBar)
.addComponent(self.sizeProgLabel))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING) #updatetime, strut, ETC
.addComponent(self.lastUpdatedLabel)
.addComponent(progRight) #invisible, just for gluing things to
.addComponent(self.ETCLabel))
.addComponent(self.clearBtn))
然而,正如我所料,这迫使进度条挤进前 2 个标签
之间的水平空间,如下所示:
最后,我想到了摆脱支柱,并将进度条添加到三个独立的平行组中:LEADING
与两个左侧标签对齐,CENTER
与中间标签对齐,与右侧标签对齐TRAILING
:
#Horizontal layout is a sequence of 4 parallel groups of components
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) #starttime, progleft, filesprog
.addComponent(startTimeLabel)
.addComponent(self.progressBar)
.addComponent(self.fileProgLabel))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER) #progmid, sizeprog
.addComponent(self.progressBar)
.addComponent(self.sizeProgLabel))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING) #updatetime, progright, ETC
.addComponent(self.lastUpdatedLabel)
.addComponent(self.progressBar)
.addComponent(self.ETCLabel))
.addComponent(self.clearBtn))
然而,Swing 显然忽略了第二次和第三次提到的进度条,我最终得到了这个:
我认为我在这方面做得相当不错,而且我完全没有想法。有没有办法让一个组件跨越多个并行组?