简体   繁体   English

将字符串复制到剪贴板不起作用(Java)

[英]Copying string to clipboard doesn't work (Java)

I'm trying to copy a string to the clipboard once user click on a proper button. 我试图在用户单击适当的按钮后将字符串复制到剪贴板。 This particular operation is supposed to be performed in a JDialog. 该特定操作应该在JDialog中执行。
Here's part of my code, where the copying should occur: 这是我的代码的一部分,应该在其中进行复制:

btnCopy.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        StringSelection selection = new StringSelection(txtarContent.getText());
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(selection, selection);

    }
});

I'm using Eclipse (neon.3) on Mac OS X (10.12.4 Sierra). 我在Mac OS X(10.12.4 Sierra)上使用Eclipse(neon.3)。


Edit based on questions in comments: 根据评论中的问题进行编辑:

  • No exception is thrown (nor are any errors/warnings) 没有引发异常(也没有任何错误/警告)
  • After an execution of the code, clipboard remains unmodified 执行代码后,剪贴板保持不变

PROBLEM SOLVED: 问题解决了:
The problem was not in the solution but in my code. 问题不在于解决方案,而在于我的代码。
It seems now that the button's listener wasn't working thus no code was executed. 现在看来,按钮的侦听器无法正常工作,因此没有执行任何代码。

That's a good lesson for me as a beginner to check even the most obvious things, like whether the listener works or not. 对于初学者来说,这对我来说是一个很好的课程,它可以检查最明显的事情,例如听者是否工作。 Thank you everyone for help. 谢谢大家的帮助。

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

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