java 从 JLabel 中选择文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/997942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:44:46  来源:igfitidea点击:

Selecting text from a JLabel?

javaswing

提问by mellis

Is it possible to enable the selection of text from a JLabel? If not, what's the best alternative control to use, and how can it be configured to appear like a JLabel?

是否可以启用从 JLabel 中选择文本的功能?如果不是,最好使用的替代控件是什么,如何将其配置为看起来像 JLabel?

回答by Arend

A JTextField doesn't allow html-formatted text like a JLabel. If you want selectable html text you could alternatively try a JTextPane set to html formatting:

JTextField 不允许像 JLabel 这样的 html 格式的文本。如果您想要可选择的 html 文本,您也可以尝试将 JTextPane 设置为 html 格式:

JTextPane f = new JTextPane();
f.setContentType("text/html"); // let the text pane know this is what you want
f.setText("<html>Hello World</html>"); // showing off
f.setEditable(false); // as before
f.setBackground(null); // this is the same as a JLabel
f.setBorder(null); // remove the border

回答by Pierre

You can use a JTextField without enabling the editing

您可以在不启用编辑的情况下使用 JTextField

JTextField f=new JTextField("Hello World");
f.setEditable(false);
content.add(f);

Pierre

皮埃尔

回答by lindon fox

Building on the answers: You can use a JTextField without enabling the editing

基于答案:您可以在不启用编辑的情况下使用 JTextField

JTextField f=new JTextField("Hello World");
f.setEditable(false);
f.setBackground(null); //this is the same as a JLabel
f.setBorder(null); //remove the border

I don't know how to stop the text from "Jumping" when you select it, or replace the text (programmatically). Maybe it is just my computer...

我不知道如何在您选择文本时停止“跳转”,或替换文本(以编程方式)。可能只是我的电脑...

回答by akf

When using JTextField, you will also want to remove the border: f.setBorder(null);

使用 JTextField 时,您还需要删除边框: f.setBorder(null);

and set the disabled text color: f.setDisabledTextColor(Color.black);

并设置禁用的文本颜色: f.setDisabledTextColor(Color.black);

回答by Alexander V.

As variant below CopyableLabel supports html tags and Fonts as JLabel.

作为下面的变体,CopyableLabel 支持 html 标签和字体作为 JLabel。

public class CopyableLabel extends JTextPane {

    private static final long serialVersionUID = -1;

    private static final Font DEFAULT_FONT;

    static {
        Font font = UIManager.getFont("Label.font");
        DEFAULT_FONT = (font != null) ? font: new Font("Tahoma", Font.PLAIN, 11);
    }

    public CopyableLabel() {
        construct();
    }

    private void construct() {
        setContentType("text/html");

        setEditable(false);
        setBackground(null);
        setBorder(null);

        putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true);
        setFont(DEFAULT_FONT);
    }
}

回答by sangretu

JLabels cannot be editable.

JLabels 不能编辑。

However, you could use a JTextField and just change the foreground / background colors to make it appear as a JLabel. If you wanted to be really fancy you could add code to change the colors when it's selected to indicate that it's editable.

但是,您可以使用 JTextField 并更改前景色/背景色以使其显示为 JLabel。如果你真的想变得更漂亮,你可以添加代码来改变颜色,当它被选中以表明它是可编辑的。

回答by Joshua Goldberg

Besides the changes suggested in other responses (setEditable, setContentType, setOpaque or setBackground, maybe setEnabled + setDisabledTextColor(Color.black), maybe setBorder(null) and/or setMargin(new Insets(0,0,0,0))

除了其他响应中建议的更改(setEditable、setContentType、setOpaque 或 setBackground,可能是 setEnabled + setDisabledTextColor(Color.black),可能是 setBorder(null) 和/或 setMargin(new Insets(0,0,0,0))

To get the font of a JTextPane to look like a JLabel, see the suggestion from this blog post:

要使 JTextPane 的字体看起来像 JLabel,请参阅此博客文章中的建议:

"Unfortunately, simply calling set font on JEditorPane will have no effect, as the default font is pulled from a style sheet rather than the JComponent. There is, however, a clever way around the errant font default. The best way to change the default font in an HTML rendering JEditorPane, is to alter the style sheet like this:"

“不幸的是,简单地在 JEditorPane 上调用 set font 将没有任何效果,因为默认字体是从样式表而不是 JComponent 中提取的。但是,有一种巧妙的方法可以绕过错误的字体默认值。更改默认值的最佳方法HTML 渲染 JEditorPane 中的字体,就是像这样改变样式表:”

    // create a JEditorPane that renders HTML and defaults to the system font.
    JEditorPane editorPane = 
            new JEditorPane(new HTMLEditorKit().getContentType(),text);
    // set the text of the JEditorPane to the given text.
    editorPane.setText(text);

    // add a CSS rule to force body tags to use the default label font
    // instead of the value in javax.swing.text.html.default.csss
    Font font = UIManager.getFont("Label.font");
    String bodyRule = "body { font-family: " + font.getFamily() + "; " +
            "font-size: " + font.getSize() + "pt; }";
    ((HTMLDocument)editorPane.getDocument()).getStyleSheet().addRule(bodyRule);