java – 使用GroupLayout,如何将单独的组件与一个较长组件的每一端对齐?或者,一个组件可以跨多个并行组吗?
作者:互联网
tl; dr:我想做第二张图片中的内容(忽略红线)
我理解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个标签之间的水平空间,如下所示:
最后,我想要摆脱struts,并将进度条添加到三个独立的并行组:使用两个左侧标签对齐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明显忽略了进度条的第二次和第三次提及,我最终得到了这个:
我估计我已经有了相当不错的表现,而且我很有想法.有没有办法让组件跨越多个并行组?
解决方法:
鉴于您的要求,我宁愿使用GridBagLayout.它有点复杂,但我写了一段代码,可以做你想要的.
class T extends JFrame {
public T() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
String s1 = "Started at: 2011-09-12 15:33:38";
String s2 = "Last updated: 2011-09-12 15:33:44";
String s3 = "File copied:2/10";
String s4 = "Bytes copied: 234/1000";
String s5 = "ETC: 2011-09-02 15:34:02";
JProgressBar progressBar = new JProgressBar();
progressBar.setMinimum(100);
progressBar.setStringPainted(true);
progressBar.setString("23%");
progressBar.setValue(23);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.insets = new Insets(5, 5, 5, 5);
add(new JLabel(s1), c);
c.gridx = 2;
add(new JLabel(s2, JLabel.RIGHT), c);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
add(progressBar, c);
c.gridx = 3;
add(new JButton("Clear"), c);
c.gridx = 0;
c.gridy = 2;
add(new JLabel(s3), c);
c.gridx = 0;
add(new JLabel(s4, JLabel.CENTER), c);
c.gridx = 2;
add(new JLabel(s5, JLabel.RIGHT), c);
setSize(600, 300);
setVisible(true);
}
public static void main(String[] args) {
new T();
}
结果如下:
标签:java,user-interface,swing,grouplayout 来源: https://codeday.me/bug/20190726/1541860.html