gpt4 book ai didi

java - 使用 GroupLayout,如何将单独的组件对齐到一个较长组件的每一端?或者,一个组件可以跨越多个并行组吗?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:15:55 25 4
gpt4 key购买 nike

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))))

产生了这个: enter image description here

但是,我想对齐进度条开始和结束的 2 个顶部标签,以及进度条开始、中间和结束的 3 个底部标签,如下所示(mspainted): enter image description here

我的第一个方法是尝试将组件拆分到我用上面的行创建的并行组中。我将 struts 放在进度条的任一侧,将 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 个标签之间的水平空间,如下所示: enter image description here

最后,我想到了去掉 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 显然忽略了进度条的第二次和第三次提及,我最终得到了这个: enter image description here

我认为我在这方面做得相当不错,但我完全没有想法。有什么办法可以让一个组件跨越多个并行组?

最佳答案

鉴于您的要求,我宁愿使用 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();
}

结果如下: screenshot

关于java - 使用 GroupLayout,如何将单独的组件对齐到一个较长组件的每一端?或者,一个组件可以跨越多个并行组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7279799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com