繁体   English   中英

如何将每个 JPanel 向左或向右对齐到主 JPanel

[英]How to alignment each JPanel to left or to right into main JPanel

我希望我的应用程序看起来像这样:

看图片

我必须尝试使用

jpanel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
jpanel.setAlignmentX(JPanel.RIGHT_ALIGNMENT);

但是当我将它添加到 main_JPanel 时,它适用于第一个 jpanel。 正确显示但是当我添加 JPanel2, JPanel3,... 然后 main_Panel 显示不正确,它显示:

错误显示 jpanel

这是我的函数创建每个 JPanel

public JPanel makeJpanel(String Mess, String username) {
        int height = 40;
        int width = 200;
        JTextPane textPane = new JTextPane();
        textPane.setEditable(false);
        StyledDocument doc = textPane.getStyledDocument();
        String rs = splitMess(Mess);
        Mess = rs.substring(2).trim();

        Style style = textPane.addStyle("I'm a Style", null);

        StyleConstants.setFontFamily(style, "Arial");
        StyleConstants.setBold(style, true);
        StyleConstants.setFontSize(style, 20);
        StyleConstants.setForeground(style, new Color(19, 51, 55));
        try {
            doc.insertString(doc.getLength(), Mess.trim(), style);
        } catch (BadLocationException e) {
        }
        height = (int) Math.round(textPane.getPreferredSize().getHeight());
        width = (int) Math.round(textPane.getPreferredSize().getWidth());

        //creat avatar
        JLabel ava = null;
        if (username.equals(this.userName)) {
            ava = new JLabel(label_myAvaProfile1.getIcon());
        } else {
            ava = new JLabel(label_avaYourFriends1.getIcon());
        }
        JLabel borderAva = new JLabel(border_label_avaYourFriends.getIcon());
        if (height < 40) {
            ava.setPreferredSize(new Dimension(40, 40));
            borderAva.setPreferredSize(new Dimension(40, 40));
        } else {
            ava.setPreferredSize(new Dimension(40, height));
            borderAva.setPreferredSize(new Dimension(40, height));
        }
        JLayeredPane lpAva = new JLayeredPane();
        lpAva.setLayout(new OverlayLayout(lpAva));
        lpAva.add(borderAva);
        lpAva.add(ava);

        JPanel panelChat = new JPanel();
        panelChat.setLayout(new BoxLayout(panelChat, BoxLayout.LINE_AXIS));
        panelChat.setBackground(Color.WHITE);
        panelChat.setPreferredSize(new Dimension(width + 40, height));
        panelChat.setMinimumSize(new Dimension(width + 40, height));
        panelChat.setMaximumSize(new Dimension(width + 40, height));
        if (username.equals(this.userName)) {
            panelChat.add(textPane);
            panelChat.add(lpAva);
            //NOTICE THIS
            panelChat.setAlignmentX(JPanel.LEFT_ALIGNMENT);
        } else if (username.equals(currentFriendUserName)) {
            panelChat.add(lpAva);
            panelChat.add(textPane);
            //NOTICE THIS
            panelChat.setAlignmentX(JPanel.RIGHT_ALIGNMENT);
        }

        resetPanelListFriends(panelChat);
        return panelChat;
    }

添加新 Jpanel 时调用的此函数

public void renderPanelChatLog()
{
    JPanel panelChat = new JPanel();
        panelChat = makeJPanel(mess, username);
        if (panelChat != null) {
            panel_ChatLog.add(panelChat);
            panel_ChatLog.add(Box.createVerticalStrut(20));
        }
        resetPanelListFriends(panel_ChatLog);
}

对不起,我的英语,我只是个新人...请帮帮我!

这是一种方法:

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

public class BoxLayoutAligned
{
    private static void createAndShowGUI()
    {
        JLabel left = new JLabel( "Left Aligned" );
        Box leftBox = Box.createHorizontalBox();
        leftBox.add( left );
        leftBox.add( Box.createHorizontalGlue() );

        JLabel right = new JLabel( "Right Aligned" );
        Box rightBox = Box.createHorizontalBox();
        rightBox.add( Box.createHorizontalGlue() );
        rightBox.add( right );

        Box vertical = Box.createVerticalBox();
        vertical.setBorder( new EmptyBorder(0, 10, 0, 10) );
        vertical.add( leftBox );
        vertical.add( rightBox );

        JFrame frame = new JFrame("BoxLayout Aligned");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(vertical);
        frame.setSize(300, 300);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

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

通过添加“胶水”,您可以让添加到 Box 的其他组件向左或向右对齐。

暂无
暂无

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

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