如何在 BoxLayout 中使组件全宽

How to make a component full width in BoxLayout

我有一个 JPanel,它列出了一组 Jlabel。我想将每个标签的宽度设置为面板的大小。所以它会很宽,但文本留在左边。我正在使用 BoxLayout 列出标签。

代码如下:

public class JavaApplication78 {
    JFrame frame;
    JPanel panel, labelsPanel;
    JLabel label;
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<JLabel> labelsArray = new ArrayList<JLabel>();
    Border paddingBorder = BorderFactory.createEmptyBorder(10,10,10,10);
    Border border = BorderFactory.createLineBorder(Color.BLUE);
    JScrollPane labelsScroll;
    public JavaApplication78(){
        frame = new JFrame();
        panel = new JPanel(new BorderLayout());
        names.add(".mp3");names.add(".html");names.add(".jpeg");names.add(".mp4");names.add(".pdf");

        labelsPanel = new JPanel();
        labelsPanel.setLayout(new BoxLayout(labelsPanel, BoxLayout.PAGE_AXIS));

        labelsScroll = new JScrollPane(labelsPanel);
        for(String format : names){
            label = new JLabel(format);
            //icon
            labelsArray.add(label);
            labelsPanel.add(label);
            label.setBorder(BorderFactory.createCompoundBorder(border,paddingBorder));
        }

        panel.add(labelsScroll, BorderLayout.CENTER);
        frame.add(panel);
        frame.setSize(200, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JavaApplication78();
    }

}

目前我可以在每个 JLabel 周围设置边框。标签的高度可以,但宽度需要与父面板一样宽。 有什么想法吗?

BoxLayout 遵守组件的最大尺寸,对于 JLabel,最大尺寸等于首选尺寸。

您可以覆盖 getMaximumSize() 方法:

label = new JLabel(format)
{
    @Override
    public Dimension getMaximumSize()
    {
        Dimension d = super.getMaximumSize();
        d.width = Integer.MAX_VALUE;
        return d;
    }
};
label.setHorizontalAlignment(JLabel.CENTER);

这将允许标签水平扩展以填充面板的宽度。

或者,如果您愿意,可以使用 Relative Layout。它的功能与 BoxLayout 非常相似,但有一些附加功能。基本代码为:

RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS);
rl.setFill( true );
JPanel labelsPanel = new JPanel( rl );

使用 PreferredSize 为您的 JLabel 设置父级 JPanel 的尺寸:

JLabel label = new JLabel() {
   public Dimension getPreferredSize() {
       return labelsPanel.getSize();
   };
};

除非你必须使用 BoxLayout,否则最好使用 @MadProgrammer 在他的评论中建议的 GridBagLayout

编辑:

你也可以看看MigLayout。用它添加组件非常简单:

labelsPanel.setLayout(new MigLayout());
labelsPanel.add(label, "span") // span to take the whole row width.

您可以使用 GridBagLayout...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;

public class JavaApplication78 {

    JFrame frame;
    JPanel panel, labelsPanel;
    JLabel label;
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<JLabel> labelsArray = new ArrayList<JLabel>();
    Border paddingBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10);
    Border border = BorderFactory.createLineBorder(Color.BLUE);
    JScrollPane labelsScroll;

    public JavaApplication78() {
        frame = new JFrame();
        panel = new JPanel(new BorderLayout());
        names.add(".mp3");
        names.add(".html");
        names.add(".jpeg");
        names.add(".mp4");
        names.add(".pdf");

        labelsPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = GridBagConstraints.REMAINDER;

        labelsScroll = new JScrollPane(labelsPanel);
        for (String format : names) {
            label = new JLabel(format);
            //icon
            labelsArray.add(label);
            labelsPanel.add(label, gbc);
            label.setBorder(BorderFactory.createCompoundBorder(border, paddingBorder));
        }

        panel.add(labelsScroll, BorderLayout.CENTER);
        frame.add(panel);
        frame.setSize(200, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                new JavaApplication78();
            }
        });
    }
}

JList,但在这种情况下,我倾向于使用 SwingLabs SwingX 库中的 VerticalLayout