How to align JLabel to the left of the JPanel?

牧云@^-^@ 提交于 2020-01-01 04:48:11

问题


I want to align my JLabel to the left.

    String lText = "<html><b><font color = white face = comic sans ms size = 20>mybook</font></b></html>";
    JLabel label = new JLabel(lText);
    label.setBorder(new EmptyBorder(25,0,25,500));

I tried to do it using EmptyBorder but it isnt aligning properly. I am using FlowLayout


回答1:


FlowLayout uses CENTER alignment by default. Try using LEFT alignment for your JLabel JPanel container

myJPanel.setLayout(new FlowLayout(FlowLayout.LEFT));



回答2:


You might wish to set the JLabel's horizontalAlignment property. One way is via its constructor. Try:

JLabel label = new JLabel(lText, SwingConstants.LEFT);

This can also be done via the expected setter method:

label.setHorizontalAlignment(SwingConstants.LEFT);


来源:https://stackoverflow.com/questions/17511518/how-to-align-jlabel-to-the-left-of-the-jpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!