这是一个例子,显示JLabel显示的时间太短,并且没有足够的空间用于"网站" (参考代码)部分。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JLabeleg extends JFrame
{
public JLabeleg()
{
setTitle("Example");
setSize(500,100);
setVisible(true);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
Container eg = getContentPane();
JPanel west = new JPanel(new GridLayout(2,1));
JPanel center = new JPanel(new GridLayout(2,3));
eg.add(west,BorderLayout.WEST);
eg.add(center,BorderLayout.CENTER);
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(""));
}
public static void main(String[] args)
{
JLabeleg example = new JLabeleg();
example.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
我的原始代码和这个例子之间有什么不同,JLabel的来源实际上是" setText"来自IO文件的对象加载。那有关系吗?无论如何,对于" www.example.com/example/example"有一个完整的视图?也许是在右手边占用JLabel的空间?
答案 0 :(得分:4)
一种解决方案是在加载标签文本后使用pack()
。我不太喜欢这个解决方案,因为我觉得这对用户来说很令人不安。
GridLayout
也会很快向您展示一些重要的限制(如果您有一个大标签而其他标签很小,则会有很多可用空间丢失,因为所有标签都会达到最大标签的大小) 。在所有情况下,我建议只设置GridLayout
的“行数”或“列数”,这样更容易维护。
GridBagLayout
可能是另一种选择,但要掌握更多经验。
最后,在您的情况下,JTable
可能是更好的选择(但我们缺乏您想要实现的目标的背景)。
以下是两个展示如何完成工作的例子(但还有许多其他可能更好的方法,解释你的目标会帮助我们更精确)。
示例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();
}
});
}
}