1

我正在制作一个我正在使用的应用程序BoxLayout。如下图所示,当标题字符串很短时,它是完美的。但是随着字符串变长,JLabel越来越错位。 图片 这是一些与问题相关的代码:

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

我有什么明显的遗漏吗?谷歌对这个问题没有帮助。

如果它很重要,国家标签进度条的东西只是JPanels 和FlowLayouts。

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。

4

2 回答 2

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);
    }
}
于 2012-11-17T14:11:37.123 回答
2
  • BoxLayout接受Min,MaxPreferredSize, 子元素可以从Minto调整大小MaxSize

  • FlowLayou只接受PreferredSize,rest ( Min, MaxSize) 被这个LayoutManager忽略,childs 不可调整大小

  • 这些是从放入容器中的孩子XxxSize计算出来的(在这种情况下)PreferredSizeJPanel

  • (您的问题)为了获得更好的帮助,请尽快发布SSCCE,简短,可运行,可编译,只是关于您的问题

于 2012-11-17T14:01:14.263 回答