challenge クリップボードへの転送

クリップボード(や同等の機能)へテキストを転送するプログラムをお願いします。 また可能でしたらクリップボードのデータを取り出すプログラムもお願いします。

システムに依存する内容ですが、応用範囲が広いと思いましたので出題させてもらいました。

Posted feedbacks - Scala

Javaと同じ処理になりますが...。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import    java.awt.BorderLayout
import    java.awt.Container
import    java.awt.Dimension
import    java.awt.FlowLayout
import    java.awt.Toolkit
import    java.awt.datatransfer.Clipboard
import    java.awt.datatransfer.DataFlavor
import    java.awt.datatransfer.StringSelection
import    java.awt.event.ActionEvent
import    java.awt.event.ActionListener
import    javax.swing.JButton
import    javax.swing.JFrame
import    javax.swing.JMenu
import    javax.swing.JMenuBar
import    javax.swing.JMenuItem
import    javax.swing.JPanel
import    javax.swing.JTextArea

class SButton(l:String,c:ActionEvent=>Unit) extends JButton with ActionListener {
    
    setText(l)
    addActionListener(this)
    
    override def actionPerformed(e:ActionEvent) = c(e)
}

class SMenuItem(l:String,c:ActionEvent=>Unit) extends JMenuItem with ActionListener {
    
    setText(l)
    addActionListener(this)
    
    override def actionPerformed(e:ActionEvent) = c(e)
}

class ClipboardFrame(title:String) extends JFrame {
    
    val    mb:JMenuBar = new JMenuBar
    val    fm:JMenu = new JMenu("file")
    val    em:JMenu = new JMenu("edit")
    val    c:Container = getContentPane
    val    t:JTextArea = new JTextArea(20,40);
    val    p:JPanel = new JPanel;
    val    clipboard:Clipboard = Toolkit.getDefaultToolkit.getSystemClipboard;
    
    c.setLayout(new BorderLayout)
    c.add(t,BorderLayout.CENTER)
    
    p.setLayout(new FlowLayout)
    p.add(new SButton("copy",copy))
    p.add(new SButton("paste",paste))
    c.add(p,BorderLayout.SOUTH)
    
    fm.add(new SMenuItem("quit",quit))
    mb.add(fm)
    em.add(new SMenuItem("copy",copy))
    em.add(new SMenuItem("paste",paste))
    mb.add(em)
    setJMenuBar(mb)
    
    setResizable(false)
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    setTitle(title)
    
    pack
    
    def this() = this("clipboad")
    
    def quit(e:ActionEvent):Unit = {
        System.exit(0)
    }
    
    def copy(e:ActionEvent):Unit = {
        val    s:StringSelection = new StringSelection(t.getSelectedText);
        clipboard.setContents(s,s);
    }
    
    def paste(e:ActionEvent):Unit = {
        t.replaceSelection(clipboard.getData(DataFlavor.stringFlavor).asInstanceOf[String])
    }
}

object Main extends Application {
    (new ClipboardFrame).setVisible(true)
}

Index

Feed

Other

Link

Pathtraq

loading...