如何将每个 JPanel 向左或向右对齐到主 JPanel
Posted
技术标签:
【中文标题】如何将每个 JPanel 向左或向右对齐到主 JPanel【英文标题】:How to alignment each JPanel to left or to right into main JPanel 【发布时间】:2020-06-24 16:23:11 【问题描述】:我希望我的应用看起来像这样:
我必须尝试使用
jpanel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
jpanel.setAlignmentX(JPanel.RIGHT_ALIGNMENT);
但是当我将它添加到 main_JPanel 时,它适用于第一个 jpanel。 Show correctly 但是当我添加 JPanel2,JPanel3,... 然后 main_Panel 显示不正确,它显示:
这是我的函数创建每个 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);
对不起,我的英语,我只是新人...请帮助我!
【问题讨论】:
以后发布minimal reproducible example 来说明您的问题。我们对您的应用程序不感兴趣,只对演示问题的代码感兴趣。您如何创建子面板的详细信息与您的问题无关。您的问题是关于对齐,而不是添加到面板的组件。我的答案是“MRE”的形式。 【参考方案1】:这是一种方法:
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 的其他组件左对齐或右对齐。
【讨论】:
以上是关于如何将每个 JPanel 向左或向右对齐到主 JPanel的主要内容,如果未能解决你的问题,请参考以下文章