| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「Jython JTextComponent 1」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
 
1行目: 1行目:
==Jython JTextComponent 1==
+
==[[Jython JTextComponent 1]]==
 
[[JTextComponent 1]] | [[Jython Swing]] | [[Swing]] | [[Jython]] | [[Python]] |  
 
[[JTextComponent 1]] | [[Jython Swing]] | [[Swing]] | [[Jython]] | [[Python]] |  
  
 
===方針===
 
===方針===
*半ば強引に、[JTextComponent 1] [Swingで作成したサンプル]を Jythonに書き換えてみる。
+
*半ば強引に、[[JTextComponent 1|Swingで作成したサンプル]]を Jythonに書き換えてみる。
  
 
===内容===
 
===内容===
 
*メニューとアクションの割付
 
*メニューとアクションの割付
 
*キーボード押下との割付
 
*キーボード押下との割付
*Undo、Redoの実装
+
*Undo、[[R]]edoの実装
 
*Documentの利用
 
*Documentの利用
 
*Documentのイベント感知
 
*Documentのイベント感知
24行目: 24行目:
 
   
 
   
 
  from javax.swing import AbstractAction, Action, InputMap
 
  from javax.swing import AbstractAction, Action, InputMap
  from javax.swing import SwingUtilities, JFrame, JMenu, JMenuBar, JScrollPane, JTextArea, KeyStroke
+
  from javax.swing import SwingUtilities, [[JFrame]], JMenu, JMenuBar, JScrollPane, JTextArea, KeyStroke
 
  from javax.swing.event import UndoableEditEvent, UndoableEditListener
 
  from javax.swing.event import UndoableEditEvent, UndoableEditListener
 
  from javax.swing.text import DefaultEditorKit, Document, JTextComponent
 
  from javax.swing.text import DefaultEditorKit, Document, JTextComponent
32行目: 32行目:
 
      
 
      
 
     def createUI(self):
 
     def createUI(self):
         frame = JFrame("TextTest")
+
         frame = [[JFrame]]("TextTest")
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
+
         frame.setDefaultCloseOperation([[JFrame]].EXIT_ON_CLOSE)
 
   
 
   
 
         txtArea = JTextArea(10, 30)
 
         txtArea = JTextArea(10, 30)
         frame.contentPane.add(JScrollPane(txtArea), BorderLayout.CENTER)
+
         frame.contentPane.add(JScrollPane(txtArea), BorderLayout.CENTE[[R]])
 
   
 
   
 
         # TextComponentが持つActionをMapに格納
 
         # TextComponentが持つActionをMapに格納
46行目: 46行目:
 
         action_param = {}
 
         action_param = {}
 
         action_param['undo'] = undo
 
         action_param['undo'] = undo
         redoAction = RedoAction(**action_param)
+
         redoAction = [[R]]edoAction(**action_param)
 
         action_param['redoAction'] = redoAction
 
         action_param['redoAction'] = redoAction
 
         undoAction = UndoAction(**action_param)
 
         undoAction = UndoAction(**action_param)
52行目: 52行目:
 
   
 
   
 
         # メニューを作成
 
         # メニューを作成
         menu = JMenu("Edit")
+
         menu = J[[Menu]]("Edit")
 
         menu.add(actionMap.get(DefaultEditorKit.cutAction))
 
         menu.add(actionMap.get(DefaultEditorKit.cutAction))
 
         menu.add(actionMap.get(DefaultEditorKit.copyAction))
 
         menu.add(actionMap.get(DefaultEditorKit.copyAction))
62行目: 62行目:
 
         menu.add(actionMap.get(DefaultEditorKit.selectAllAction))
 
         menu.add(actionMap.get(DefaultEditorKit.selectAllAction))
 
   
 
   
         mb = JMenuBar()
+
         mb = J[[Menu]]Bar()
 
         mb.add(menu)
 
         mb.add(menu)
         frame.JMenuBar = mb
+
         frame.J[[Menu]]Bar = mb
 
   
 
   
         # Documentのイベント感知(Undo Redo 管理を行う)
+
         # Documentのイベント感知(Undo [[R]]edo 管理を行う)
 
         doc = txtArea.document
 
         doc = txtArea.document
 
         doc.addUndoableEditListener(UndoEventHandler(**action_param))
 
         doc.addUndoableEditListener(UndoEventHandler(**action_param))
 
   
 
   
         # テキストのUndo、Redoにキーを割り当てる (Ctrl+z,Ctrl+y)
+
         # テキストのUndo、[[R]]edoにキーを割り当てる (Ctrl+z,Ctrl+y)
 
         inputMap = txtArea.inputMap
 
         inputMap = txtArea.inputMap
         undoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.CTRL_MASK)
+
         undoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.CT[[R]]L_MASK)
         redoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Y,Event.CTRL_MASK)
+
         redoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Y,Event.CT[[R]]L_MASK)
 
         inputMap.put(undoKey, undoAction)
 
         inputMap.put(undoKey, undoAction)
 
         inputMap.put(redoKey, redoAction)
 
         inputMap.put(redoKey, redoAction)
 
   
 
   
 
         frame.pack()
 
         frame.pack()
         frame.visible = True
+
         frame.[[vi]]sible = True
 
   
 
   
 
  class UndoEventHandler(UndoableEditListener):
 
  class UndoEventHandler(UndoableEditListener):
104行目: 104行目:
 
             ex.printStackTrace()
 
             ex.printStackTrace()
 
         self.updateState()
 
         self.updateState()
         # Redo を可能に
+
         # [[R]]edo を可能に
 
         self.redoAction.updateState()
 
         self.redoAction.updateState()
 
   
 
   
110行目: 110行目:
 
         self.setEnabled(self.undo.canUndo())
 
         self.setEnabled(self.undo.canUndo())
 
   
 
   
  class RedoAction(AbstractAction):
+
  class [[R]]edoAction(AbstractAction):
 
     def __init__(self, undo):
 
     def __init__(self, undo):
         super(RedoAction, self).__init__("Redo")
+
         super([[R]]edoAction, self).__init__("[[R]]edo")
 
         self.undo = undo
 
         self.undo = undo
 
         self.enabled = False
 
         self.enabled = False
124行目: 124行目:
 
   
 
   
 
     def updateState(self):
 
     def updateState(self):
         self.setEnabled(self.undo.canRedo())
+
         self.setEnabled(self.undo.can[[R]]edo())
 
   
 
   
  class Invoker(Runnable):
+
  class Invoker([[R]]unnable):
 
     def run(self):
 
     def run(self):
 
         jtt = JTextTest()
 
         jtt = JTextTest()
 
         jtt.createUI()
 
         jtt.createUI()
 
   
 
   
  SwingUtilities.invokeLater(Invoker())
+
  [[Swing]]Utilities.invokeLater(Invoker())

2020年2月16日 (日) 04:28時点における最新版

Jython JTextComponent 1

JTextComponent 1 | Jython Swing | Swing | Jython | Python |

方針

内容

  • メニューとアクションの割付
  • キーボード押下との割付
  • Undo、Redoの実装
  • Documentの利用
  • Documentのイベント感知

ソースコード

実行例

0749 js jtext.jpg

ソースコード
# -*- coding: utf-8 -*-
 
from java.lang import *
from java.awt import BorderLayout,Event
from java.awt.event import ActionEvent, KeyEvent
from java.util import HashMap, Map 

from javax.swing import AbstractAction, Action, InputMap
from javax.swing import SwingUtilities, JFrame, JMenu, JMenuBar, JScrollPane, JTextArea, KeyStroke
from javax.swing.event import UndoableEditEvent, UndoableEditListener
from javax.swing.text import DefaultEditorKit, Document, JTextComponent
from javax.swing.undo import UndoManager
 
class JTextTest(object):
    
    def createUI(self):
        frame = JFrame("TextTest")
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

        txtArea = JTextArea(10, 30)
        frame.contentPane.add(JScrollPane(txtArea), BorderLayout.CENTER)

        # TextComponentが持つActionをMapに格納
        actionMap = {}
        for action in txtArea.actions:
            actionMap[action.getValue(Action.NAME)] = action

        undo = UndoManager()
        action_param = {}
        action_param['undo'] = undo
        redoAction = RedoAction(**action_param)
        action_param['redoAction'] = redoAction
        undoAction = UndoAction(**action_param)
        action_param['undoAction'] = undoAction

        # メニューを作成
        menu = JMenu("Edit")
        menu.add(actionMap.get(DefaultEditorKit.cutAction))
        menu.add(actionMap.get(DefaultEditorKit.copyAction))
        menu.add(actionMap.get(DefaultEditorKit.pasteAction))
        menu.addSeparator()
        menu.add(undoAction)
        menu.add(redoAction)
        menu.addSeparator()
        menu.add(actionMap.get(DefaultEditorKit.selectAllAction))

        mb = JMenuBar()
        mb.add(menu)
        frame.JMenuBar = mb

        # Documentのイベント感知(Undo Redo 管理を行う)
        doc = txtArea.document
        doc.addUndoableEditListener(UndoEventHandler(**action_param))

        # テキストのUndo、Redoにキーを割り当てる (Ctrl+z,Ctrl+y)
        inputMap = txtArea.inputMap
        undoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.CTRL_MASK)
        redoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Y,Event.CTRL_MASK)
        inputMap.put(undoKey, undoAction)
        inputMap.put(redoKey, redoAction)

        frame.pack()
        frame.visible = True

class UndoEventHandler(UndoableEditListener):
        def __init__(self, undo, undoAction, redoAction):
            self.undo = undo
            self.undoAction = undoAction
            self.redoAction = redoAction
            
        def undoableEditHappened(self, e):
            self.undo.addEdit(e.edit)
            self.undoAction.updateState()
            self.redoAction.updateState()

class UndoAction(AbstractAction):
    def __init__(self, undo, redoAction):
        super(UndoAction, self).__init__("Undo")
        self.undo = undo
        self.redoAction = redoAction
        self.enabled = False

    def actionPerformed(self, e):
        try:
            self.undo.undo()
        except Exception, ex:
            ex.printStackTrace()
        self.updateState()
        # Redo を可能に
        self.redoAction.updateState()

    def updateState(self):
        self.setEnabled(self.undo.canUndo())

class RedoAction(AbstractAction):
    def __init__(self, undo):
        super(RedoAction, self).__init__("Redo")
        self.undo = undo
        self.enabled = False

    def actionPerformed(self, e):
        try:
            self.undo.redo()
        except Exception, ex:
            ex.printStackTrace()
        self.updateState()

    def updateState(self):
        self.setEnabled(self.undo.canRedo())

class Invoker(Runnable):
    def run(self):
        jtt = JTextTest()
        jtt.createUI()

SwingUtilities.invokeLater(Invoker())