简体   繁体   中英

How do I right align text within a JLabel?

I have the following code:

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

for(int xx =0; xx < 3; xx++)
{
    JLabel label = new JLabel("String");
    label.setPreferredSize(new Dimension(300,15));
    label.setHorizontalAlignment(JLabel.RIGHT);

    panel.add(label);
}

This is how I would like the text to look:

[                         String]
[                         String]
[                         String]

this is how it looks

[String]
[String]
[String]

For some reason label doesn't get set to the preferred size I specified and I think because of this it doesn't right align my label text. But im not sure. Any help would be appreciated.

JLabel label = new JLabel("String", SwingConstants.RIGHT);

:)

I think it depend to layout that you are using, in XY (I remember was some kind of layouts in JBuilder) it should work, but in other can be problem. Try to change minimum size to prefered size.

The setPreferredSize/MinimumSize/MaximumSize methods is dependent from the layout manager of the parent component (in this case panel).

First try with setMaximumSize instead of setPreferredSize, if I'm not going wrong should work with BoxLayout.

In addition: probably you have to use and play around with glues:

panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(Box.createHorizontalGlue());
panel.add(label);
panel.add(Box.createHorizontalGlue());

If you need the Y_AXIS BoxLayout you could also used nested panel:

verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS));    
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(Box.createHorizontalGlue());
panel.add(label);
panel.add(Box.createHorizontalGlue());
verticalPanel.add(panel);

This is a bit annoying, but you could use nested JPanels with box layouts if you wanted more flexibility in your alignment than with grid layout.

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));


    for (int xx = 0; xx < 3; xx++) {
        JPanel temp = new JPanel();
        temp.setLayout(new BoxLayout(temp,BoxLayout.LINE_AXIS));

        JLabel label = new JLabel("String");
        temp.add(Box.createHorizontalGlue());

        temp.add(label);
        panel.add(temp);
    }

I used horizontal glue to keep it at the right no matter the size, but you can put in rigid areas to make it a specific distance.

You need to ensure that your LayoutManager is sizing the label to fill the target area. You probably have a JLabel component which is exactly sized to the length of the text and which has been left aligned in the layout.

myLabel#setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

rather than use

label.setHorizontalAlignment(JLabel.RIGHT);

use

label.setHorizontalAlignment(SwingConstants.RIGHT);

thus you have:

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for(int xx =0; xx < 3; xx++)
{
    JLabel label = new JLabel("String");
    label.setPreferredSize(new Dimension(300,15));
    label.setHorizontalAlignment(SwingConstants.RIGHT);
    panel.add(label);
}

Couldn't you use the following?

Jlabel label = new JLabel("String");
label.setBounds(x, y, width, height); // <-- Note the different method used.
label.setHorizontalAlignment(JLabel.RIGHT);

This works within a JFrame Container at least. Not sure about a JPanel .

With the responses from you guys I was able to determine that BoxLayout doesn't support the text alignment that I wanted, so I changed it to

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,1,0,0);

and everything worked fine.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM