gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:02:13 28 4
gpt4 key购买 nike

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

View Image

我必须尝试使用​​

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

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

Error show 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 的其他组件向左或向右对齐。

关于java - 如何将每个 JPanel 向左或向右对齐到主 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60657134/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com