Bottom-to-top layout in Java with dynamic sizing

烈酒焚心 提交于 2019-12-12 05:27:45

问题


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".

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.

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

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);
}

}

回答1:


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/

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

You can define your layout like this:

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.



来源:https://stackoverflow.com/questions/22346858/bottom-to-top-layout-in-java-with-dynamic-sizing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!