没有首选/最大/最小尺寸的文本包装 JLabels

发布于 2024-11-06 19:12:19 字数 2174 浏览 0 评论 0原文

我使用 JLabels 作为超链接。我希望它们的外观和功能与浏览器中的链接完全相同,因此使用文本窗格/编辑器窗格等是不可取的。问题是,许多将放置这些链接的容器没有指定大小:它们依赖于其父级(甚至祖父母)的大小。

我使用框进行布局,如果链接文本的宽度超过框宽度的三分之二,则框内容开始延伸到框的实际边界之外(这样,如果该框被 JScrollPane 包围) ,会出现水平滚动条)。

有没有办法强制 JLabel 换行?我读过的许多帖子都说,将文本放入 HTML 标签中会导致它在必要时自动换行 - 但无论我做什么,我都无法让它工作。我无法使用
标签,因为我不知道它们到底会去哪里,除非这可以自动化,即:

if(text.length() > wrapLength)
{
    //break the text into substrings and insert a <br /> at wrapLength
    //the difficulty is in figuring out the value of wrapLength
}

这是超链接类的代码:

import java.awt.Color;
import java.awt.Desktop;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.JLabel;

public class Hyperlink extends JLabel
{
    private URI uri;
    private String baseText;

    public Hyperlink(String text, String uri, Font font, Color textColor)
    {
        super(text);
        baseText = text;
        setFont(font);
        setForeground(textColor);
        setOpaque(false);

        if(Desktop.isDesktopSupported())
        {
            try
            {
                setToolTipText(uri);
                this.uri = new URI(uri);
                addMouseListener(new LinkMouseListener());
            }
            catch(URISyntaxException ex)
            {
            }
        }
    }

    private class LinkMouseListener extends MouseAdapter
    {
        @Override
        public void mouseClicked(MouseEvent e)
        {
            Desktop desktop = Desktop.getDesktop();
            try
            {
                desktop.browse(uri);
            }
            catch(IOException ioe)
            {
                System.out.println("Something went wrong..."); //DELETE
            }
        }

        @Override
        public void mouseEntered(MouseEvent e)
        {
            setText("<html><u>" + baseText + "</u></html>");
        }

        @Override
        public void mouseExited(MouseEvent e)
        {
            setText(baseText);
        }
    }
}

I'm using JLabels as hyperlinks. I want them to look and function exactly like a link in a browser, so using text panes/editor panes etc is not desirable. The problem is that many of the containers these links will be placed in do not have a size specified: they rely on the size of their parents (or even grand-parents).

I am using Boxes for layouts, and if the link text spans more than about two-thirds the width of the box, the box contents begin to stretch outside the actual bounds of the box (so that, if that box were surrounded by a JScrollPane, a horizontal scrollbar would appear).

Is there any way to enforce wrapping on a JLabel? Many posts I have read say that putting the text in HTML tags will cause it to wrap automatically when necessary - but no matter what I do I cannot get this to work. I can't use <br /> tags because I do not know exactly where they would go unless this could be automated, i.e.:

if(text.length() > wrapLength)
{
    //break the text into substrings and insert a <br /> at wrapLength
    //the difficulty is in figuring out the value of wrapLength
}

Here is the code for the hyperlink class:

import java.awt.Color;
import java.awt.Desktop;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.JLabel;

public class Hyperlink extends JLabel
{
    private URI uri;
    private String baseText;

    public Hyperlink(String text, String uri, Font font, Color textColor)
    {
        super(text);
        baseText = text;
        setFont(font);
        setForeground(textColor);
        setOpaque(false);

        if(Desktop.isDesktopSupported())
        {
            try
            {
                setToolTipText(uri);
                this.uri = new URI(uri);
                addMouseListener(new LinkMouseListener());
            }
            catch(URISyntaxException ex)
            {
            }
        }
    }

    private class LinkMouseListener extends MouseAdapter
    {
        @Override
        public void mouseClicked(MouseEvent e)
        {
            Desktop desktop = Desktop.getDesktop();
            try
            {
                desktop.browse(uri);
            }
            catch(IOException ioe)
            {
                System.out.println("Something went wrong..."); //DELETE
            }
        }

        @Override
        public void mouseEntered(MouseEvent e)
        {
            setText("<html><u>" + baseText + "</u></html>");
        }

        @Override
        public void mouseExited(MouseEvent e)
        {
            setText(baseText);
        }
    }
}

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

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

发布评论

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

评论(2

芯好空 2024-11-13 19:12:19

在标签上,尝试 setPreferredSize(new Dimension(width, height))setSize(new Dimension(width, height))setMaximumSize(new Dimension(width) , height)) 并将宽度设置为您希望其换行的宽度。我认为在你的情况下它将是 setMaximumSize(...)。

由于布局管理器的原因,它不会自动换行。某些布局管理器会忽略首选尺寸或其他尺寸。在您的情况下,您可能只需要使用不同的 setXXXSize() 方法并找到您的布局管理器所遵循的方法。

要获得首选宽度,您必须从父母之一获取宽度。再次强调,根据布局管理器,您可能需要获取首选宽度、最大宽度或 getSize().getWidth()。如果不了解有关布局的更多信息,很难说您到底需要如何执行此操作,但您确实需要在标签上设置某种宽度以使其换行 - 没有办法解决这个问题。

我能想到的唯一其他选择是将标签上的首选宽度设置为您知道应该始终适合可用空间的任意值。

On the label, try setPreferredSize(new Dimension(width, height)), setSize(new Dimension(width, height)) or setMaximumSize(new Dimension(width, height)) and set the width to the width where you want it to wrap. I think in your case it will be setMaximumSize(...).

It doesn't wrap automatically because of your layout manager. Some layout managers will ignore the preferred size or other dimensions. In your case, you probably just need to play around with the different setXXXSize() methods and find the one that is respected by your layout manager.

To get the preferred width, you're going to have to get the width from one of the parents. Once again, depending on the layout manager, you might have to get the preferred width, maximum width or getSize().getWidth(). Without knowing more about the layout, it's difficult to say how exactly you need to do this, but you do need to set some kind of width on the label for it to wrap - There's no way around that.

The only other option I can think of would be to set the preferred width on the label to something arbitrary that you know should always fit in the available space.

时光是把杀猪刀 2024-11-13 19:12:19

使用 JTextArea。文本可以设置为自动换行。您可以使用文本区域属性,使其看起来像标签。

您可以使用自定义荧光笔为文本添加“下划线”。然后您只需根据需要添加/删除荧光笔即可。 矩形绘制器可以帮助您入门。

Use a JTextArea. The text can be set to wrap automatically. You can play with the text area properties to make it look like a label.

You can use a custom Highlighter to "underline" the text. Then you just add/remove the highlighter as necessary. The Rectangle Painter can get you started.

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