Java从底部到顶部的布局,具有动态大小调整 [英] Bottom-to-top layout in Java with dynamic sizing

查看:127
本文介绍了Java从底部到顶部的布局,具有动态大小调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用底部填充的聊天气泡"构建一个通用的聊天界面.我是Java新手,无法弄清楚要使用哪个布局管理器.我尝试过使用BoxLayout和顶部的某些胶水来向下推动内部JPanel,但是这迫使我在它们上设置了"setMaximumSize",这阻止了基于气泡"中文本的大小调整.

I am trying to build a common chat interface with chat-"bubbles" that fills up from the bottom. I am new to Java and can't figure out which Layout Manager to use. I have tried using the BoxLayout and some Glue at the top to push down the inner JPanels, but that forces me to set a "setMaximumSize" on them, which prevents the resizing based on the text inside the "bubbles".

您可以在上面看到预期的结果.每个气泡行都是一个JPanel.他们都是对齐的底部.应允许气泡随着文本内容扩展到最大MAX,然后将其自动换行.即使气泡变为多行,userimage也应始终在顶部对齐.但是,时间戳记应与气泡中间"对齐.

You can see the expected result above. Each bubble-row is a JPanel. They are all aligned bottom. The bubbles should be allowed to expand withe the text-content up to a certain MAX, then it should word-wrap. The userimage should always be aligned top, even if the bubble becomes multi-row. The timestamp however, should be "middle" aligned to the bubble.

任何帮助将不胜感激.我认为我应该使用其他布局管理器,例如MigLayout而不是BoxLayout?

Any help would be appreciated. I assume I should use some other Layout Manager, like MigLayout instead of BoxLayout?

更新1: 这似乎可行:

UPDATE 1: This seems to be working:

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.JPanel;

import javax.swing.BoxLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Panel;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;


public class FormTest extends JFrame {

public static void main(String[] args) {
    new FormTest();
}

@SuppressWarnings("deprecation")
public FormTest() {

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Test");
    this.setSize(1000, 500);
    this.setMinimumSize(new Dimension(700, 300));
    this.setLocationRelativeTo(null);

    //Bubble panel
    JPanel bubble = new JPanel();
    bubble.setBackground(Color.RED);
    bubble.setLayout(new BoxLayout(bubble, BoxLayout.X_AXIS));

    //Container panel
    JPanel container = new JPanel();
    container.setBackground(Color.CYAN);

    FormLayout layout = new FormLayout("fill:default:grow", "fill:default:grow");
    PanelBuilder builder = new PanelBuilder(layout);
    CellConstraints cc = new CellConstraints();

    layout.appendRow(new RowSpec("pref"));
    builder.add(bubble, cc.xy(1, layout.getRowCount()));        

    container.setLayout(layout);

    //Add container panel to window
    getContentPane().add(builder.getPanel(), BorderLayout.CENTER);

    JLabel lblAaaa = new JLabel("<html>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</html>");
    bubble.add(lblAaaa);

    //Create the window
    this.setVisible(true);
}

}

推荐答案

如果您使用的是Swing,则可以尝试使用jgoodies的表单布局. 在这里看看: http://www.jgoodies.com/freeware/libraries/forms/

if you are using Swing you can try to use form-layout from jgoodies for this. Have a look here: http://www.jgoodies.com/freeware/libraries/forms/

首先定义您的间隔符("fill:default:grow"),然后将面板动态添加到底部

First define your spacer ("fill:default:grow") and then dynamically add your panels to the bottom

您可以这样定义布局:

FormLayout layout = new FormLayout("file:default:grow","fill:default:grow");
PanelBuilder builder = new PanelBuilder(layout);
CellConstraints cc = new CellConstraints();

layout.appendRow(new RowSpec("pref"));
buiulder.add(new Panel(), cc.xy(1,layout.getRowCount()));

我已经在包含聊天功能的演示应用程序中使用了此方法.

I used this method already in demo application which had a chat included.

这篇关于Java从底部到顶部的布局,具有动态大小调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆