java-Swing-布局管理器出现问题

我正在尝试使用Java Swing制作聊天应用程序,但聊天气泡遇到了麻烦.我设法做起泡的部分,但似乎无法正确放置它们.这是我的代码

            package messagebubble;

import java.awt.BorderLayout;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import messagebubble.RoundedBorder;

public class Main {

public static void main(String[] args) {

    JFrame window = new JFrame();

    JPanel chat = new JPanel();
    chat.setLayout(new BoxLayout(chat, BoxLayout.PAGE_AXIS));

    JPanel painel = new JPanel(new BorderLayout());
    JPanel painel2 = new JPanel(new BorderLayout());

    JTextArea msg = new JTextArea();
    msg.setLineWrap(true);
    msg.setWrapStyleWord(true);

    msg.setText("ola tudo bem meus caros amigos, como estão? há muito que não vos via por estas bandas haha lol");
    msg.setBorder(new RoundedBorder());
    msg.setColumns(msg.getLineCount());

    JTextArea msg2 = new JTextArea();
    msg2.setLineWrap(true);
    msg2.setWrapStyleWord(true);

    msg2.setText("ola tudo bem");
    msg2.setBorder(new RoundedBorder());

    painel.add(msg,BorderLayout.NORTH);

    chat.add(painel);
    painel2.add(msg2,BorderLayout.NORTH);

    chat.add(painel2);

    window.add(chat);

    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(400, 500);
    window.setVisible(true);



    }

}

这是圆形边框

package GUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;

import javax.swing.border.AbstractBorder;

public class RoundedBorder extends AbstractBorder {



@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width,
        int height) {

    Graphics2D graph = (Graphics2D) g.create();
    RoundRectangle2D round = new RoundRectangle2D.Float(x, y, width - 1,
            height - 1, 30, 30);
    Container parent = c.getParent();

    if (parent != null) {
        graph.setColor(parent.getBackground());
        Area canto = new Area(new Rectangle2D.Float(x,y,width,height));
        canto.subtract(new Area(round));
        graph.fill(canto);
    }

    graph.setColor(Color.GRAY);
    graph.draw(round);
    graph.dispose();

}

@Override
public Insets getBorderInsets(Component c) {
    return new Insets(5, 10, 5, 10);
}

@Override
public Insets getBorderInsets(Component c, Insets insets) {
    insets.left = 10;
    insets.right = insets.left;
    insets.top = 5;
    insets.bottom = insets.top;
    return insets;
}

}

输出看起来像这样Output.我无法摆脱消息之间的空白或底部JTextArea中的空白.

java-Swing-布局管理器出现问题

我还尝试将面板的最大尺寸设置为他们喜欢的尺寸,看起来像是太窄的Output2.

java-Swing-布局管理器出现问题

知道我该如何解决吗?我想要达到的是这样的ideal

java-Swing-布局管理器出现问题

解决方法:

我会推荐

>使用单个BorderLayout容纳所有组件
>让它在BorderLayout.PAGE_START位置容纳另一个使用BoxLayout PAGE_AXIS的JPanel.
>将您的聊天添加到BoxLayout JPanel.
>请注意,我们大多数人都没有圆角矩形边框实用程序,因此无法对此进行测试.

例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;


public class MainPanel extends JPanel {

    private static final int PREF_W = 400;
    private static final int PREF_H = 500;
    private JPanel msgHoldingPanel = new JPanel();


    public MainPanel() {
        JTextArea msg = new JTextArea();
        msg.setLineWrap(true);
        msg.setWrapStyleWord(true);

        msg.setText("ola tudo bem meus caros amigos, como estão? há muito que não vos via por estas bandas haha lol");

        // !! msg.setBorder(new RoundedBorder());
        msg.setBorder(BorderFactory.createLineBorder(Color.blue));

        msg.setColumns(msg.getLineCount());

        JTextArea msg2 = new JTextArea();
        msg2.setLineWrap(true);
        msg2.setWrapStyleWord(true);

        msg2.setText("ola tudo bem");
        msg2.setBorder(BorderFactory.createLineBorder(Color.blue));

        msgHoldingPanel.setLayout(new BoxLayout(msgHoldingPanel, BoxLayout.PAGE_AXIS));
        msgHoldingPanel.add(msg);
        msgHoldingPanel.add(Box.createVerticalStrut(3));
        msgHoldingPanel.add(msg2);

        setLayout(new BorderLayout());
        add(msgHoldingPanel, BorderLayout.PAGE_START);
    }


    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("MainPanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}
上一篇:PHP-快速,可靠地更新聊天室的最佳方法?


下一篇:机器学习基石笔记:13 Hazard of Overfitting