Java 无法从JLable复制/粘贴

Java 无法从JLable复制/粘贴,java,jframe,jlabel,Java,Jframe,Jlabel,我正在使用下面的代码显示一个消息对话框: String msg = "<html>You need to download it from here: <br><b> ttp://chromedriver.storage.googleapis.com/index.html?path=2.20/ </b <br></html>"; JLabel label = new JLabel(msg); label.setFont(new j

我正在使用下面的代码显示一个消息对话框:

String msg = "<html>You need to download it from here: <br><b> ttp://chromedriver.storage.googleapis.com/index.html?path=2.20/ </b <br></html>";
JLabel label = new JLabel(msg);
label.setFont(new java.awt.Font("serif", java.awt.Font.PLAIN, 14));
JOptionPane.showMessageDialog(null, label);

但是用户无法复制和粘贴链接,

所以,有点小技巧不太好,但也不太好

使用绝地武士窗格


我很想在JLabel中添加一个MouseListener,然后在鼠标左键上单击,简单地跟随链接,但这就是我

您考虑过使用一个底层JTextField吗?还有一些超链接组件实现漂浮在webs@MadProgrammer是的,但我不喜欢它的样子like@Reimeus请解释更多?忘记了JTextField不呈现HTML:P
public class TestPane extends JPanel {

    public TestPane() {
        JEditorPane field = new JEditorPane();
        field.setContentType("text/html");
        field.setText("<html><a href='https://google.com'>Google it</a></html>");
        field.setEditable(false);
        field.setBorder(null);
        field.setOpaque(false);
        setLayout(new GridBagLayout());
        add(field);
    }

}
public class TestPane extends JPanel {

    public TestPane() {
        JLabel field = new JLabel("<html><a href='https://google.com'>Google it</a></html>");
        field.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        add(field, gbc);
        add(new JTextField(20), gbc);

        JPopupMenu menu = new JPopupMenu();
        menu.add(new CopyAction("https://google.com"));
        menu.add(new OpenAction("https://google.com"));
        field.setComponentPopupMenu(menu);
    }

    public class CopyAction extends AbstractAction {

        private String url;

        public CopyAction(String url) {
            super("Copy");
            this.url = url;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            Toolkit tk = Toolkit.getDefaultToolkit();
            Clipboard cb = tk.getSystemClipboard();
            cb.setContents(new StringSelection(url), null);
        }

    }

    public class OpenAction extends AbstractAction {

        private String url;

        public OpenAction(String url) {
            super("Follow");
            this.url = url;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            Desktop desktop = Desktop.getDesktop();
            if (desktop.isSupported(Action.BROWSE)) {
                try {
                    desktop.browse(new URL(url).toURI());
                } catch (IOException | URISyntaxException ex) {
                    ex.printStackTrace();
                }
            }
        }

    }

}