3

这是一个 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 的源实际上是来自 IO 文件的对象加载的“setText”。这有关系吗?无论如何,“www.example.com/example/example”是否有完整的视图?也许通过占用右侧 JLabel 的空间?

4

1 回答 1

4

一种解决方案是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(使用GridBagLayoutrevalidate()

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();
            }
        });

    }
}
于 2013-06-06T09:25:37.267 回答