我遇到GroupLayout
的一些问题。
在运行时禁用元素时,手动间隔似乎已损坏。
元素之间存在巨大的间隙,而使用自动间隙时根本就不存在。
自动间隙:
标签已启用:
标签1-8停用:
手动差距:
标签已启用:
标签1-8停用:
是一个错误,还是我忘记了一些设置?
这里是根据代码:
public class GroupLayoutTesting extends JFrame{
public static void main(String[] args) {
new GroupLayoutTesting();
}
public GroupLayoutTesting() {
super();
this.getContentPane().setLayout(new BorderLayout());
this.setVisible(true);
this.setSize(new Dimension(600,100));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().add(getGroupLayouted());
}
static JLabel[] labels = {
new JLabel("Label 0"),
new JLabel("Label 1"),
new JLabel("Label 2"),
new JLabel("Label 3"),
new JLabel("Label 4"),
new JLabel("Label 5"),
new JLabel("Label 6"),
new JLabel("Label 7"),
new JLabel("Label 8"),
new JLabel("Label 9")
};
private Component getGroupLayouted() {
JPanel panel = new JPanel();
GroupLayout layout = new GroupLayout(panel);
//layout.setAutoCreateGaps(true); // <- automatic gaps
panel.setLayout(layout);
JButton btn = new JButton("Light switch");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(int i = 1; i<9; i++){
labels[i].setVisible(!labels[i].isVisible());
}
}
});
SequentialGroup sequentialGroup = layout.createSequentialGroup();
layout.setHorizontalGroup(sequentialGroup);
sequentialGroup.addComponent(btn);
sequentialGroup.addPreferredGap(ComponentPlacement.UNRELATED,10,10); //<- Manual gaps
for(int i=0; i<10; i++) {
sequentialGroup.addComponent(labels[i]);
sequentialGroup.addPreferredGap(ComponentPlacement.UNRELATED,10,10); //<- Manual gaps
}
ParallelGroup parallelGroup = layout.createParallelGroup();
layout.setVerticalGroup(parallelGroup);
parallelGroup.addComponent(btn);
for(int i=0; i<10; i++) {
parallelGroup.addComponent(labels[i]);
}
return panel;
}
}
最佳答案
我什么都没看到。间隙是在容器内分配组件的指南。在没有特定原因的情况下,我会坚持使用自动间距,因为“它们会自动adjust运行UI的外观,反映出实际的外观准则”。
作为参考,我更新了您的示例,如下所示。根据经验,手册GAP
的0
匹配我平台上的自动间隙,但不匹配容器间隙。另请参见Initial Threads。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
public class GroupLayoutTesting extends JFrame {
private static final int GAP = 0;
public GroupLayoutTesting() {
this.getContentPane().setLayout(new BorderLayout());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.add(getGroupLayouted());
this.pack();
this.setVisible(true);
}
static JLabel[] labels = {
new JLabel("Label-0"),
new JLabel("Label-1"),
new JLabel("Label-2"),
new JLabel("Label-3"),
new JLabel("Label-4"),
new JLabel("Label-5"),
new JLabel("Label-6"),
new JLabel("Label-7"),
new JLabel("Label-8"),
new JLabel("Label-9")
};
private JPanel getGroupLayouted() {
JPanel panel = new JPanel();
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
JButton btn = new JButton("Light switch");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 1; i < labels.length - 1; i++) {
labels[i].setVisible(!labels[i].isVisible());
}
}
});
SequentialGroup sequentialGroup = layout.createSequentialGroup();
layout.setHorizontalGroup(sequentialGroup);
sequentialGroup.addComponent(btn);
//sequentialGroup.addPreferredGap(ComponentPlacement.UNRELATED, GAP, GAP); //<- Manual gaps
for (int i = 0; i < 10; i++) {
sequentialGroup.addComponent(labels[i]);
//sequentialGroup.addPreferredGap(ComponentPlacement.UNRELATED, GAP, GAP); //<- Manual gaps
}
ParallelGroup parallelGroup = layout.createParallelGroup();
layout.setVerticalGroup(parallelGroup);
parallelGroup.addComponent(btn);
for (int i = 0; i < 10; i++) {
parallelGroup.addComponent(labels[i]);
}
return panel;
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new GroupLayoutTesting();
});
}
}