Java JLabel / JButton:在某些系统上,我得到“......” (省略号),在某些系统上,我没有。如何强制禁用省略号? [英] Java JLabel/JButton: on some systems I get "..." (an ellipsis) and on some systems I don't. how can I force to disable the ellipsis at all?

查看:333
本文介绍了Java JLabel / JButton:在某些系统上,我得到“......” (省略号),在某些系统上,我没有。如何强制禁用省略号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数系统上,我的JLabel中的内容显示正常。它的存在方式应该总是大到足以显示其内容文本,因为我基本上这样做:

On most systems, the content in my JLabel just shows fine. It is also resided in a way that it should be always big enough to show its content text because I basically do this:

label.setText(text);
label.setFont(new Font(fontName, 0, 12));
int width = label.getFontMetrics(label.getFont()).stringWidth(text);
int height = 21; // this should always be enough
label.setBounds(new Rectangle(x, y, width, height));

但是在某些系统上(不是我自己的,所以我无法真正调试它那么容易),它削减了文本并在结尾显示...。

But on some systems (not my own so I cannot really debug it that easy), it cuts the text and shows "..." at the end.

您可以看到完整代码 here ,您可以看到示例这里(Abbildungen_Bijektiv_X3)

You can see the full code here and you can see the example here (Abbildungen_Bijektiv_X3).

我也有JButton的一些类似案例。

I also have some similar case for JButton.

如何强制Swing不这样做呢? (即使它认为组件太小了。)

How can I force Swing to not do that? (Even if it thinks that the component is too small.)

Swing究竟在哪里处理这个?我浏览了 JButton 的代码和一些相关的类,但我没有真正找到它切割文本的代码并添加了省略号。

Where exactly does Swing handle this? I browsed through the code of JButton and some related classes but I didn't really found the code where it cuts the text and adds the ellipsis.

推荐答案

我现在正在这样做(对于按钮,你可以用类似的方式对其他控件进行):

I am doing this now (for buttons but you could do it in a similar way for other controls):

static public class ButtonUI extends MetalButtonUI {
    public static ComponentUI createUI(JComponent c) {
        return new ButtonUI();
    }

    @Override public void paint(Graphics g, JComponent c) {
        JSimpleLabel.activateAntiAliasing(g);

        AbstractButton b = (AbstractButton) c;
        ButtonModel model = b.getModel();

        String text = b.getText();
        clearTextShiftOffset();

        // perform UI specific press action, e.g. Windows L&F shifts text
        if (model.isArmed() && model.isPressed()) {
            paintButtonPressed(g,b); 
        }

        FontMetrics metrics = g.getFontMetrics();
        Rectangle2D stringBounds = metrics.getStringBounds(text, g);
        g.drawString(text,
                (b.getWidth() - (int)stringBounds.getWidth()) / 2,
                metrics.getLeading() + metrics.getMaxAscent() + (b.getHeight() - (int)stringBounds.getHeight()) / 2);

        if (b.isFocusPainted() && b.hasFocus()) {
            Rectangle viewRect = new Rectangle();
            final int inset = 1;
            viewRect.x = inset;
            viewRect.y = inset;
            viewRect.width = b.getWidth() - (inset + viewRect.x) - 1;
            viewRect.height = b.getHeight() - (inset + viewRect.y) - 1;
            g.setColor(getFocusColor());
            g.drawRect(viewRect.x, viewRect.y, viewRect.width, viewRect.height);
        }
    }       
}

public void init() {
    try {
        UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel() {
            private static final long serialVersionUID = 1L;
            @Override public UIDefaults getDefaults() {
                UIDefaults table = super.getDefaults();
                table.put("ButtonUI", ButtonUI.class.getName());
                return table;
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
    // ...
}

这篇关于Java JLabel / JButton:在某些系统上,我得到“......” (省略号),在某些系统上,我没有。如何强制禁用省略号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆