如何右对齐 JLabel 中的图标?
对于带有图标的 JLabel,如果您setHorizontalTextPosition(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用:
label1.setHorizontalTextPosition(SwingConstants.LEFT);
(设置文本相对于图标的位置)
You should use:
label1.setHorizontalTextPosition(SwingConstants.LEFT);
(Set the position of the text, relative to the icon)
这是想要的效果吗?
附录:我认为小组是可行的方法。
Is this the desired effect?
Addendum: I think a panel is the way to go.
我找到了一种更简单的方法来做到这一点。我需要在 JTable 中拥有这种布局,并通过获取文本宽度然后手动设置文本和图标之间的宽度来进行正确的调整。我为我的 JTable 子类化了 DefaultTableCellRenderer,
效果非常好!
是的,对于真正的代码,应该检查文本宽度是否大于 DESIREDWIDTH。
对于没有固定宽度的自动右对齐,适用于可变宽度的列:
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
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: