Java Jlabel/jbutton:在某些系统上,我得到了“ ...” (一个省略号),在某些系统上,我没有。我如何强制禁用省略号?

发布于 2025-01-22 08:56:33 字数 850 浏览 3 评论 0原文

在大多数系统上,我的jlabel中的内容都很好。它的居住在某种程度上,它应该总是足够大以显示其内容文本,因为我基本上是这样做的:但是,

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

在某些系统上(不是我自己的系统,所以我真的不能轻松调试它),它剪切了文本并显示“” ...“ 在最后。

您可以看到完整的代码您可以看到示例

我也有一些类似的案例。

我该如何强迫摇摆不这样做? (即使认为组件太小。)

摇摆到底在哪里处理这个?我浏览了jbutton和一些相关类的代码,但我并没有真正找到剪切文本并添加省略号的代码。

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.

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

I also have some similar case for JButton.

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

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.

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

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

发布评论

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

评论(3

安稳善良 2025-01-29 08:56:33

不需要设置标签的界限。

那是布局经理的工作。学习使用布局经理,您将不会有这个问题。

编辑:

布局经理使用:

label.setSize( label.getPreferredSize() );

There should be no need to set the bounds of the label.

That is the job of a layout manager. Learn to use layout managers and you won't have this problem.

Edit:

Layout managers use:

label.setSize( label.getPreferredSize() );
梦里南柯 2025-01-29 08:56:33

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

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();
    }
    // ...
}

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();
    }
    // ...
}
无敌元气妹 2025-01-29 08:56:33

您可以使用跨平台的外观和感觉(就像Nimbus一样)来停止这种情况

You could use a cross platform look and feel (Like Nimbus) to stop this occuring

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