如何在 JLabel 中右对齐文本?

发布于 2024-11-14 13:02:33 字数 611 浏览 4 评论 0原文

我有以下代码:

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

这就是我希望文本的外观:

[                         String]
[                         String]
[                         String]

这就是它的外观

[String]
[String]
[String]

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

微凉徒眸意 2024-11-21 13:02:33
JLabel label = new JLabel("String", SwingConstants.RIGHT);

:)

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

:)

淡淡绿茶香 2024-11-21 13:02:33

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

首先尝试使用 setMaximumSize 而不是 setPreferredSize,如果我没有出错的话应该使用 BoxLayout。

另外:可能你必须使用和使用胶水:

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

如果你需要 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);

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);
烙印 2024-11-21 13:02:33

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

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.

司马昭之心 2024-11-21 13:02:33

这有点烦人,但如果您希望比网格布局更灵活地对齐,您可以使用带有框布局的嵌套 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);
    }

我使用水平胶水将其保持在正确的位置,无论大小如何,但您可以放置​​刚性区域以使其具有特定的距离。

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.

坏尐絯 2024-11-21 13:02:33

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

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.

迷你仙 2024-11-21 13:02:33
myLabel#setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
myLabel#setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
木緿 2024-11-21 13:02:33

而不是使用

label.setHorizontalAlignment(JLabel.RIGHT);

use

label.setHorizontalAlignment(SwingConstants.RIGHT);

因此你有:

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

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);
}
往事风中埋 2024-11-21 13:02:33

不能用下面的吗?

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

这至少可以在 JFrame 容器中运行。不确定 JPanel

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.

跨年 2024-11-21 13:02:33

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

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,1,0,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

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

and everything worked fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文