我编写了一个简单的scala swing应用程序,带有文本字段和按钮。每当用户点击按钮时,内容就会被复制到剪贴板。但是,一旦应用程序关闭,剪贴板内容就会丢失。 为什么?有办法解决这个问题吗?
这是我的源代码:
package com.loloof64.scalatest
import java.awt.Dimension
import java.awt.Toolkit
import java.awt.datatransfer.Clipboard
import java.awt.datatransfer.StringSelection
import java.awt.datatransfer.Transferable
import scala.swing.BoxPanel
import scala.swing.Button
import scala.swing.MainFrame
import scala.swing.SimpleSwingApplication
import scala.swing.TextField
import scala.swing.Orientation
import scala.swing.event.ButtonClicked
object ClipboardCopyTest extends SimpleSwingApplication {
def writeToClipboard(str:String) {
val clipboard:Clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
val transferData:Transferable = new StringSelection(str)
clipboard.setContents(transferData, null)
}
override def top = new MainFrame {
val textfield = new TextField(60)
val button = new Button("Copy to clipboard")
contents = new BoxPanel(Orientation.Vertical){
contents += (textfield, button)
listenTo(button)
reactions += {
case _:ButtonClicked => writeToClipboard(textfield.text)
}
}
val prefSize = new Dimension(300,200)
override def size = prefSize
}
}
欢迎帮助
我在ubuntu 13.10 64位下 我有scala 2.10.3