简体   繁体   English

我应该使用什么布局管理器

[英]What layout manager should I use

I am making a chat in java and need to display old messages in a JPanel.我正在用 Java 进行聊天,需要在 JPanel 中显示旧消息。 I need an image and the message that was being sent/received to be displayed, each on its own row.我需要一个图像和正在发送/接收的消息来显示,每个都在自己的行上。 The code that I currently have :我目前拥有的代码:

JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel container = new JPanel();
container.setPreferredSize(new Dimension(300, 400));

// Printing five messages
for (int i = 0; i < 5; i++) {
    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(300, 40));
    p.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

    JLabel img = new JLabel("Image : ");
    JLabel txt = new JLabel("This is some text");

    p.add(img);
    p.add(txt);

    img.setAlignmentX(Component.LEFT_ALIGNMENT);
    txt.setAlignmentX(Component.LEFT_ALIGNMENT);

    container.add(p);
}

f.add(container);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true); 

Result :结果 :
在此处输入图片说明

Right now I am specifying the width and height of each message which is not so good as it should automatically resize to its content.现在我正在指定每条消息的宽度和高度,这不太好,因为它应该自动调整其内容的大小。 I feel like there should be a good layout-manager for this but I am new to swing so help is appreciated as I do not know which one to use.我觉得应该有一个很好的布局管理器,但我是 Swing 的新手,因此感谢帮助,因为我不知道该使用哪个。

it should automatically resize to its content.它应该自动调整其内容的大小。

Having text that wraps to a new line is the main problem here.文本换行是这里的主要问题。

One way might be to:一种方法可能是:

  1. use vertical Box - it allows each component to have a different height使用垂直 Box - 它允许每个组件具有不同的高度
  2. wrap the text in HTML - it will allow for the text to wrap用 HTML 包装文本 - 它将允许文本换行

Something like:就像是:

import java.awt.*;
import javax.swing.*;

public class Chat extends JPanel
{
    private Box messageBox = Box.createVerticalBox();

    public Chat()
    {
        setLayout( new BorderLayout() );
        add(messageBox, BorderLayout.PAGE_START);

        addMessage("Short message");
        addMessage("A longer message that should wrap as reqired onto another line. This should happen dynamically");
    }

    public void addMessage(String text)
    {
        JPanel messagePanel = new JPanel( new BorderLayout() );

        JLabel label = new JLabel( new ImageIcon("about16.gif") );
        messagePanel.add(label, BorderLayout.LINE_START);

        JLabel message = new JLabel("<html>" + text + "</html>");
        messagePanel.add(message);

        messageBox.add(messagePanel);
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("Chat");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new Chat());
        frame.pack();
        frame.setSize(200, 100);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM