如何在JLabel中右对齐文本?

时间:2023-01-27 20:24:08

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.

由于某种原因,标签没有设置为我指定的首选大小,我认为因为它没有正确对齐我的标签文本。但我不确定。任何帮助,将不胜感激。

9 个解决方案

#1


5  

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

setPreferredSize / MinimumSize / MaximumSize方法取决于父组件的布局管理器(在本例中为面板)。

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

首先尝试使用setMaximumSize而不是setPreferredSize,如果我没有出错应该使用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:

如果你需要Y_AXIS BoxLayout,你也可以使用嵌套面板:

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);

#2


13  

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

:)

:)

#3


5  

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.

我认为这取决于您正在使用的布局,在XY中(我记得在JBuilder中是某种布局)它应该可以工作,但在其他情况下可能会有问题。尝试将最小尺寸更改为首选尺寸。

#4


3  

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。

    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.

无论大小如何,我都使用水平胶将其保持在右侧,但是你可以放入刚性区域使其达到特定的距离。

#5


2  

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.

您需要确保LayoutManager调整标签大小以填充目标区域。您可能有一个JLabel组件,其大小与文本的长度完全相同,并且在布局中保持对齐。

#6


2  

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

#7


1  

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);
}

#8


1  

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.

这至少在JFrame容器中有效。不确定JPanel。

#9


0  

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

根据你们的回复,我能够确定BoxLayout不支持我想要的文本对齐方式,所以我将其更改为

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

and everything worked fine.

一切都很好。

#1


5  

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

setPreferredSize / MinimumSize / MaximumSize方法取决于父组件的布局管理器(在本例中为面板)。

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

首先尝试使用setMaximumSize而不是setPreferredSize,如果我没有出错应该使用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:

如果你需要Y_AXIS BoxLayout,你也可以使用嵌套面板:

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);

#2


13  

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

:)

:)

#3


5  

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.

我认为这取决于您正在使用的布局,在XY中(我记得在JBuilder中是某种布局)它应该可以工作,但在其他情况下可能会有问题。尝试将最小尺寸更改为首选尺寸。

#4


3  

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。

    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.

无论大小如何,我都使用水平胶将其保持在右侧,但是你可以放入刚性区域使其达到特定的距离。

#5


2  

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.

您需要确保LayoutManager调整标签大小以填充目标区域。您可能有一个JLabel组件,其大小与文本的长度完全相同,并且在布局中保持对齐。

#6


2  

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

#7


1  

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);
}

#8


1  

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.

这至少在JFrame容器中有效。不确定JPanel。

#9


0  

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

根据你们的回复,我能够确定BoxLayout不支持我想要的文本对齐方式,所以我将其更改为

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

and everything worked fine.

一切都很好。