BoxLayout的对齐问题

时间:2012-11-17 13:24:53

标签: java swing alignment boxlayout preferredsize

我正在制作一个我正在使用BoxLayout的应用程序。正如您在下图中所看到的,当标题字符串很短时,它是完美的。但随着字符串变长,JLabel变得越来越不对齐。 image 以下是与问题相关的一些代码:

JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
frame.add(centerPanel, BorderLayout.CENTER);
//...
JLabel l = new JLabel(/*...*/);
l.setHorizontalAlignment(SwingConstants.CENTER); //I tried removing and adding
                                                 //this but nothing changed
centerPanel.add(l);

有什么东西显而易见我错过了吗?谷歌没有帮助解决这个问题。

如果重要的话,国家/地区标签进度条只是JPanel s FlowLayout s。

SSCCE:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class SSCCE {

    public static void main(String[] args) {
        final JFrame f = new JFrame("SSCCE");
        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
        final JLabel[] titles = new JLabel[5];
        JPanel[] smallPanels = new JPanel[titles.length];
        for (int i = 0; i < smallPanels.length; i ++) {
            titles[i] = new JLabel(Math.random() < 0.5 ? "foo" : "bar");
            p.add(titles[i]);
            smallPanels[i] = new JPanel();
            smallPanels[i].add(new JLabel("foobar"));
            smallPanels[i].add(new JProgressBar());
            p.add(smallPanels[i]);
        }
        f.add(p, BorderLayout.CENTER);
        final JTextField tf = new JTextField("foobar");
        tf.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                titles[2].setText(tf.getText());
                f.repaint();
            }
        });
        f.add(tf, BorderLayout.NORTH);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(600, 600);
        f.setVisible(true);
    }

}

要操作SSCCE,请在文本字段中键入内容并按Enter键。

2 个答案:

答案 0 :(得分:3)

以下是带有GridBagLayout的SSCCE的更新版本。不确定在标签或帧大小更改时您希望组件如何调整大小,但管理它不应该太难。

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;

public class SSCCE {

    public static void main(String[] args) {
        final JFrame f = new JFrame("SSCCE");
        JPanel p = new JPanel();
        p.setLayout(new GridBagLayout());
        Insets insets = new Insets(3, 3, 3, 3);
        GridBagConstraints gbc1 = new GridBagConstraints();
        gbc1.gridwidth = GridBagConstraints.REMAINDER;
        gbc1.anchor = GridBagConstraints.CENTER;
        gbc1.insets = insets;
        GridBagConstraints gbc2 = new GridBagConstraints();
        gbc2.anchor = GridBagConstraints.EAST;
        gbc2.insets = insets;
        GridBagConstraints gbc3 = new GridBagConstraints();
        gbc3.weightx = 1.0;
        gbc3.fill = GridBagConstraints.HORIZONTAL;
        gbc3.gridwidth = GridBagConstraints.REMAINDER;
        gbc3.insets = insets;
        final JLabel[] titles = new JLabel[5];
        Random random = new Random();
        for (int i = 0; i < titles.length; i++) {
            titles[i] = new JLabel(Math.random() < 0.5 ? "foo" : "bar");
            p.add(titles[i], gbc1);
            p.add(new JLabel("foobar"), gbc2);
            JProgressBar progress = new JProgressBar();
            progress.setStringPainted(true);
            progress.setString(String.valueOf(random.nextInt(100)));
            p.add(progress, gbc3);
        }
        f.add(p, BorderLayout.CENTER);
        final JTextField tf = new JTextField("foobar");
        tf.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                titles[2].setText(tf.getText());
                f.repaint();
            }
        });
        f.add(tf, BorderLayout.NORTH);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
}

答案 1 :(得分:2)

  • BoxLayout接受MinMaxPreferredSize,可以将子项从Min调整为MaxSize

  • FlowLayou仅接受PreferredSize,其余(MinMaxSize)会被此LayoutManager忽略,子项无法调整大小< / p>

  • 这些XxxSize是根据放置在容器中的孩子PreferredSize来计算的(在这种情况下为JPanel

  • (您的问题)可以更快地发布SSCCE,简短,可运行,可编辑,仅关于您的问题