在 Java 为什么我得到 java.lang.IllegalStateException: 无法打开系统剪贴板

In Java why do i get java.lang.IllegalStateException: cannot open system clipboard

在 Java 为什么我得到 java.lang.IllegalStateException: 无法打开系统剪贴板?

这是 运行 在 Windows 使用 Java 1.8.0_181 25.181-b13

java.lang.IllegalStateException: cannot open system clipboard
    at sun.awt.windows.WClipboard.openClipboard(Native Method)
    at sun.awt.datatransfer.SunClipboard.getClipboardFormatsOpenClose(SunClipboard.java:327)
    at sun.awt.datatransfer.SunClipboard.isDataFlavorAvailable(SunClipboard.java:188)
    at com.jthink.songkong.ui.startdialog.editsongs.EditSongsTable$PasteAction.flavorsChanged(EditSongsTable.java:170)
    at sun.awt.datatransfer.SunClipboardSunFlavorChangeNotifier.run(SunClipboard.java:441)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access0(EventQueue.java:97)
    at java.awt.EventQueue.run(EventQueue.java:709)
    at java.awt.EventQueue.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

PasteAction 方法是

public PasteAction(JTable tbl)
        {
            putValue(NAME, TextLabel.PASTEBUTTON.getMsg());
            putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_V, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
            table = tbl;

            final Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();

            cb.addFlavorListener(new FlavorListener()
            {
                @Override
                public void flavorsChanged(FlavorEvent e)
                {
                    try
                    {
                        setEnabled(cb.isDataFlavorAvailable(CellTransferable.CELL_DATA_FLAVOR)
                                || cb.isDataFlavorAvailable(DataFlavor.stringFlavor));
                    }
                    catch(IllegalArgumentException iae)
                    {
                        MainWindow.logger.log(Level.SEVERE, iae.getMessage(), iae);
                    }
                }
            });
            setEnabled(cb.isDataFlavorAvailable(CellTransferable.CELL_DATA_FLAVOR)
                    || cb.isDataFlavorAvailable(DataFlavor.stringFlavor));
        }

当 Java 应用程序收到通知它已失去所有权时会发生这种情况,因此有时需要允许剪贴板有少量时间 "ready." 通过添加非常短的睡眠命令到方法,我们将能够避免运行时异常。请到下面link了解更多信息

https://coderanch.com/t/377833/java/listen-clipboard