如何右对齐 JLabel 中的图标?

发布于 2024-09-03 05:41:02 字数 1440 浏览 3 评论 0原文

对于带有图标的 JLabel,如果您setHorizo​​ntalTextPosition(SwingConstants.LEADING),则无论标签有多宽,图标都会绘制在文本之后。

这对于列表来说尤其糟糕,因为图标会到处都是,具体取决于每个项目的文本长度。

我跟踪了代码,似乎在 SwingUtilities#layoutCompoundLabelImpl 中,文本宽度只是设置为 SwingUtilities2.stringWidth(c, fm, text),图标 x 是设置为跟随文本而不考虑标签宽度。

这是最简单的情况:

import java.awt.*;
import javax.swing.*;

public class TestJLabelIcon
{
    public static void main(String args[])
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                JLabel c = new JLabel("abc");
                c.setHorizontalTextPosition(SwingConstants.LEADING);
                c.setHorizontalAlignment(SwingConstants.LEADING);
                c.setIcon(UIManager.getIcon("FileChooser.detailsViewIcon"));
                c.setBorder(BorderFactory.createLineBorder(Color.RED));

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                frame.getContentPane().add(c);    
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

您可以看到标签始终填充框架,但图标保持不变。如果将两个参数都设置为 TRAILING,则会出现镜像问题。

我知道我可以覆盖 UI,或者使用 JPanel 等。我只是想知道我是否在 JLabel 中遗漏了一些简单的东西。如果不是,那么这似乎是一个 Java 错误。

仅供参考,这是 Windows XP 上的 jdk1.6.0_06。

For a JLabel with icon, if you setHorizontalTextPosition(SwingConstants.LEADING), the icon is painted right after text, no matter how wide the label is.

This is particularly bad for a list, as the icons would be all over the place depending on how long the text is for each item.

I traced the code and it seems to be that in SwingUtilities#layoutCompoundLabelImpl, text width is simply set to SwingUtilities2.stringWidth(c, fm, text), and icon x is set to follow text without considering label width.

Here is the simplest case:

import java.awt.*;
import javax.swing.*;

public class TestJLabelIcon
{
    public static void main(String args[])
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                JLabel c = new JLabel("abc");
                c.setHorizontalTextPosition(SwingConstants.LEADING);
                c.setHorizontalAlignment(SwingConstants.LEADING);
                c.setIcon(UIManager.getIcon("FileChooser.detailsViewIcon"));
                c.setBorder(BorderFactory.createLineBorder(Color.RED));

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                frame.getContentPane().add(c);    
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

You can see that label always fills the frame but icon stays put. You'll get the mirror problem if you set both arguments to TRAILING.

I know I can override the UI, or use a JPanel, etc. I just wonder if I'm missing something simple in JLabel. If not, it seems like a Java bug.

FYI this is jdk1.6.0_06 on Windows XP.

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

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

发布评论

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

评论(3

很快妥协 2024-09-10 05:41:02

您应该使用:

label1.setHorizo​​ntalTextPosition(SwingConstants.LEFT);

(设置文本相对于图标的位置)

You should use:

label1.setHorizontalTextPosition(SwingConstants.LEFT);

(Set the position of the text, relative to the icon)

二智少女 2024-09-10 05:41:02

这是想要的效果吗?

附录:我认为小组是可行的方法。

图片

import java.awt.*;
import javax.swing.*;

public class TestJLabelIcon {

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setLayout(new GridLayout(0, 1));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(createPanel("abc"));
                frame.add(createPanel("defghij"));
                frame.add(createPanel("klmn"));
                frame.add(createPanel("opq"));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

            private JPanel createPanel(String s) {
                JPanel p = new JPanel(new BorderLayout());
                p.add(new JLabel(s, JLabel.LEFT), BorderLayout.WEST);
                Icon icon = UIManager.getIcon("FileChooser.detailsViewIcon");
                p.add(new JLabel(icon, JLabel.RIGHT), BorderLayout.EAST);
                p.setBorder(BorderFactory.createLineBorder(Color.blue));
                return p;
            }
        });
    }
}

Is this the desired effect?

Addendum: I think a panel is the way to go.

image

import java.awt.*;
import javax.swing.*;

public class TestJLabelIcon {

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setLayout(new GridLayout(0, 1));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(createPanel("abc"));
                frame.add(createPanel("defghij"));
                frame.add(createPanel("klmn"));
                frame.add(createPanel("opq"));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

            private JPanel createPanel(String s) {
                JPanel p = new JPanel(new BorderLayout());
                p.add(new JLabel(s, JLabel.LEFT), BorderLayout.WEST);
                Icon icon = UIManager.getIcon("FileChooser.detailsViewIcon");
                p.add(new JLabel(icon, JLabel.RIGHT), BorderLayout.EAST);
                p.setBorder(BorderFactory.createLineBorder(Color.blue));
                return p;
            }
        });
    }
}
開玄 2024-09-10 05:41:02

我找到了一种更简单的方法来做到这一点。我需要在 JTable 中拥有这种布局,并通过获取文本宽度然后手动设置文本和图标之间的宽度来进行正确的调整。我为我的 JTable 子类化了 DefaultTableCellRenderer,

public class FixedWidthRenderer extends DefaultTableCellRenderer 
{
    ...
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, 
        boolean isSelected, boolean hasFocus, int row, int column)
    {
        ...
        FontMetrics met = super.getFontMetrics(super.getFont());
        int width = met.stringWidth(super.getText());                
        super.setIconTextGap(DESIREDWIDTH - width); 
        ...
    }
}

效果非常好!
是的,对于真正的代码,应该检查文本宽度是否大于 DESIREDWIDTH。


对于没有固定宽度的自动右对齐,适用于可变宽度的列:

    @Override
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);
        if (getIcon() != null) {
            int textWidth = getFontMetrics(getFont()).stringWidth(getText());
            Insets insets = getInsets();
            int iconTextGap = width - textWidth - getIcon().getIconWidth() - insets.left - insets.right - PADDING;
            setIconTextGap(iconTextGap);
        } else {
            setIconTextGap(0);
        }
    }

I found a much easier way to do this. I needed to have this kind of layout in a JTable, and did the right justification by getting the text width and then manually setting the width between the text and the icon. I subclassed a DefaultTableCellRenderer for my JTable

public class FixedWidthRenderer extends DefaultTableCellRenderer 
{
    ...
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, 
        boolean isSelected, boolean hasFocus, int row, int column)
    {
        ...
        FontMetrics met = super.getFontMetrics(super.getFont());
        int width = met.stringWidth(super.getText());                
        super.setIconTextGap(DESIREDWIDTH - width); 
        ...
    }
}

Works great!
And yes, for real code one should check that the text width is not bigger than the DESIREDWIDTH.


For automatic right-alignment without a fixed width that works with columns of variable width:

    @Override
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);
        if (getIcon() != null) {
            int textWidth = getFontMetrics(getFont()).stringWidth(getText());
            Insets insets = getInsets();
            int iconTextGap = width - textWidth - getIcon().getIconWidth() - insets.left - insets.right - PADDING;
            setIconTextGap(iconTextGap);
        } else {
            setIconTextGap(0);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文