【问题标题】:Alignment issues with BoxLayoutBoxLayout 的对齐问题
【发布时间】:2012-11-06 00:41:07
【问题描述】:

我正在创建一个我使用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);

我有什么明显的遗漏吗? Google 没有帮助解决这个问题。

如果它很重要,国家标签进度条的东西只是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。

【问题讨论】:

  • 我猜是对齐有问题,看看这个,希望对你有帮助docs.oracle.com/javase/tutorial/uiswing/layout/…
  • @Zara 我确实试过了。什么都没发生
  • 好吧,实际上在这个链接中,他们讨论了和你一样的问题。
  • @Zara 是的,但我试过了,但什么也没发生,正如你在我上一条评论中看到的那样。
  • SSCCE 1+ 和以下两个答案!

标签: java swing alignment boxlayout preferredsize


【解决方案1】:

这是带有 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);
    }
}

【讨论】:

  • 谢谢,这也有帮助:)
  • @PicklishDoorknob 这就是为什么要发布 SSCCE 的原因,然后 1. 任何建议都需要几分钟(+1 Guillaume Polet),2. 避免任何来自未知代码的混乱,3. 一个GBC 的替代品是 SpringLayout、自定义 MigLayout 或 TableLayout
【解决方案2】:
  • BoxLayout 接受MinMaxPreferredSize,子元素的大小可以从Min 调整为MaxSize

  • FlowLayout 只接受PreferredSize,其余(MinMaxSize)被此LayoutManager 忽略,子级不可调整大小

  • 这些XxxSize 是从PreferredSize 计算而来的,这些PreferredSize 来自放置在容器中的孩子(在本例中为JPanel

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

【讨论】:

  • 所以...我应该用FlowLayoutJLabels 包装在JPanel 中?
  • 哦耶!那解决了它!谢谢。
猜你喜欢
  • 1970-01-01
  • 2021-11-20
  • 2017-05-20
  • 2014-04-30
  • 1970-01-01
  • 2012-06-18
  • 2012-07-28
  • 2012-03-01
  • 2013-02-04
相关资源
最近更新 更多