I wanna my app look like this:
I have to try use
jpanel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
jpanel.setAlignmentX(JPanel.RIGHT_ALIGNMENT);
But it's working for first jpanel when I add it into main_JPanel. Show correctly But when I add the JPanel2, JPanel3,... then main_Panel show not correctly, it show this:
This my function create each 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;
}
This function I call when i add new 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);
}
Sorry My English, I'm just new guy... Plzz help me!
Here is one way:
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() );
}
}
By adding "glue" you allow the other components added to the Box to be aligned to the left or right.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.