无法从 JLable copy/paste

Cannot copy/paste from JLable

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

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

但用户无法复制和粘贴 link、

所以,有点 "hack"(不是真的,但也不是很好)...

利用 JEditorPane...

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

}

然后您也可以使用 Hyperlink in JEditorPane 之类的东西来实际跟随 link

另一种方法可能是为 JLabel

提供 JPopupMenu

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

    }

}

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