一种解决方案是pack()
在加载标签文本后使用。我不太喜欢这个解决方案,因为我发现它让用户感到不安。
GridLayout
还将很快向您展示一些重要的限制(如果您有一个大标签而其他标签非常小,则会丢失大量可用空间,因为所有标签都会获得最大标签的大小)。在所有情况下,我都建议只设置“行”的数量或“列”的数量GridLayout
,这样更容易维护。
GridBagLayout
可能是另一种选择,但需要更多经验才能掌握。
最后,JTable
在您的情况下,a 可能是一个更好的选择(但我们缺乏您想要实现的目标的背景)。
这里有 2 个示例,展示了如何完成工作(但还有许多其他可能更好,解释您的目标将帮助我们更准确)。
示例 1(使用pack()
和GridLayout
):
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JLabeleg extends JFrame {
private List<JLabel> labels = new ArrayList<JLabel>();
public JLabeleg() {
setTitle("Example");
setLayout(new BorderLayout());
Container eg = getContentPane();
JPanel west = new JPanel(new GridLayout(2, 1));
final JPanel center = new JPanel(new GridLayout(0, 3));
JButton load = new JButton("Load labels");
load.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int i = 0;
for (JLabel label : labels) {
label.setText("A very very very long long long long label " + String.valueOf(++i));
}
JLabeleg.this.pack();
JLabeleg.this.setLocationRelativeTo(null);
}
});
eg.add(west, BorderLayout.WEST);
eg.add(center, BorderLayout.CENTER);
eg.add(load, BorderLayout.SOUTH);
west.add(new JLabel("Name : "));
west.add(new JLabel("Website : "));
center.add(new JLabel("Szekuns"));
center.add(new JLabel(""));
center.add(new JLabel(""));
center.add(new JLabel("www.example.com.my/example/example"));
center.add(new JLabel(""));
center.add(new JLabel(""));
for (int i = 0; i < 12; i++) {
JLabel label = new JLabel();
center.add(label);
labels.add(label);
}
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JLabeleg();
}
});
}
}
示例 2(使用GridBagLayout
和revalidate()
)
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JLabeleg extends JFrame {
private List<JLabel> labels = new ArrayList<JLabel>();
public JLabeleg() {
setTitle("Example");
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container eg = getContentPane();
JPanel west = new JPanel(new GridLayout(0, 2));
final JPanel center = new JPanel(new GridBagLayout());
JButton load = new JButton("Load labels");
load.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int i = 0;
for (JLabel label : labels) {
label.setText("A very long label " + String.valueOf(++i));
}
center.revalidate();
}
});
eg.add(west, BorderLayout.WEST);
eg.add(center, BorderLayout.CENTER);
eg.add(load, BorderLayout.SOUTH);
west.add(new JLabel("Name : "));
west.add(new JLabel("Szekuns"));
west.add(new JLabel("Website : "));
west.add(new JLabel("www.example.com"));
GridBagConstraints gbc = new GridBagConstraints();
GridBagConstraints endOfRow = new GridBagConstraints();
endOfRow.gridwidth = GridBagConstraints.REMAINDER;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
JLabel label = new JLabel();
if (j + 1 < 4) {
center.add(label, gbc);
} else {
center.add(label, endOfRow);
}
labels.add(label);
}
}
setSize(1000, 300);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JLabeleg();
}
});
}
}