简体   繁体   English

从JLabel中选择文本?

[英]Selecting text from a JLabel?

Is it possible to enable the selection of text from a JLabel? 是否可以从JLabel中选择文本? If not, what's the best alternative control to use, and how can it be configured to appear like a JLabel? 如果没有,那么使用什么是最好的替代控件,以及如何将其配置为像JLabel一样?

A JTextField doesn't allow html-formatted text like a JLabel. JTextField不允许像JLabel这样的html格式的文本。 If you want selectable html text you could alternatively try a JTextPane set to html formatting: 如果你想要可选择的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

You can use a JTextField without enabling the editing 您可以在不启用编辑的情况下使用JTextField

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

Pierre 皮埃尔

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... 也许这只是我的电脑......

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

As variant below CopyableLabel supports html tags and Fonts as JLabel. 作为下面的变体,CopyableLabel支持html标签和Fonts作为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);
    }
}

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. 但是,您可以使用JTextField,只需更改前景/背景颜色,使其显示为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. 如果你想真正想要的话,你可以添加代码来改变颜色,当它被选中以表明它是可编辑的。

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字体将没有任何效果,因为默认字体是从样式表而不是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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM