我想让我的应用看起来像这样:
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中的其他组件向左或向右对齐。