【问题标题】:Adding components one after another to JPanel horizontally [closed]将组件依次水平添加到JPanel [关闭]
【发布时间】:2014-10-28 10:21:13
【问题描述】:

我在添加到 JPanel 的组件对齐时遇到问题。我正在开发一个聊天应用程序。应该使用哪种布局来达到预期目标。我尝试了所有的布局,但我没有得到想要的。大多数问题发生在调整窗口大小时。此外,从包含的图像中了解我想要实现的目标。 提前致谢。 在此处输入图片说明

【问题讨论】:

  • 为了尽快获得更好的帮助,请发帖 Minimal Complete Example 向我们展示您的尝试。
  • 就布局建议而言,使用BoxLayout 会相当容易(我只用了几分钟就做了一个模拟)。使用GridBagLayout 会多一些工作,但可能看起来会更好一些。
  • 我的票投给了 GridBagLayout,使用 gridwidth 设置为 GridBagConstraints.REMAINDER 并使用适当的锚点会产生预期的结果

标签: java swing layout alignment


【解决方案1】:

我使用BoxLayout 制作了一个原型,除了我很少使用它并想尝试它之外没有其他原因。通常情况下,GridBagLayout 是我的首选。

编辑:添加图片(感谢垃圾神!)

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class MessageAppDemo implements Runnable
{
  private String[] messages = new String[] {
      "Hello?",
      "Hey, what's up?",
      "Where are you?",
      "Right behind you.",
      "Stop following me!",
      "But you owe me money.",
      "I'll gladly repay you on Tuesday.",
      "You said that last week!",
      "But now I actually have a job."
  };
  private int msgCounter = 0;
  private JPanel panel;
  private JScrollPane scrollPane;
  private Timer timer;

  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new MessageAppDemo());
  }

  public void run()
  {
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    scrollPane = new JScrollPane(panel);
    scrollPane.setVerticalScrollBarPolicy(
        JScrollPane.VERTICAL_SCROLLBAR_NEVER);
    scrollPane.setAutoscrolls(true);

    JFrame frame = new JFrame("Message App");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(260, 180);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    timer = new Timer(1500, new ActionListener()
    {
      public void actionPerformed(ActionEvent event)
      {
        if (msgCounter < messages.length)
        {
          addMessage(messages[msgCounter]);
        }
        else
        {
          timer.stop();
        }
      }
    });
    timer.start();
  }

  private void addMessage(String text)
  {
    boolean rightAligned = msgCounter % 2 != 0;
    Color color = rightAligned ? Color.CYAN : Color.ORANGE;

    JLabel label = new JLabel(text);
    label.setOpaque(true);
    label.setBackground(color);
    label.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createLineBorder(Color.BLUE),
        BorderFactory.createEmptyBorder(2,4,2,4)
    ));

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    p.setBorder(BorderFactory.createEmptyBorder(2,4,2,4));

    if (rightAligned)
    {
      p.add(Box.createHorizontalGlue());
      p.add(label);
    }
    else
    {
      p.add(label);
      p.add(Box.createHorizontalGlue());
    }

    panel.add(p);
    panel.revalidate();
    int x = panel.getPreferredSize().width;
    int y = panel.getPreferredSize().height;
    panel.scrollRectToVisible(new Rectangle(x-1 ,y-1, 1, 1));

    msgCounter++;
  }
}

【讨论】:

    猜你喜欢
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多