《Jython2.5》The Swing Tutorial, ToolBarDemoProject
The Swing Tutorial - Jython 版《記事一覧》
《事例》 ToolBarDemoProject
《著》小粒ちゃん《監修》小泉ひよ子とタマゴ倶楽部
第1版♪2003/05/23 ● 第2版♪2009/04/03
■ 概要
公式サイト《The Swing Tutorial》で公開されている例題を、Jython で再構成したものを公開します。
ここでは、スタッフの研修用に作成した例題(サンプル集)を公開します。 原則として、オリジナル版の趣旨を尊重しつつ、Jython の特徴を活かすように改編しています。 なお、プログラムの不具合については「小粒ちゃん」にお問い合わせください。 《Note》JPython1.1.x/Jython2.1.x 用に作成したセミナー課題を、Jython2.5 で再構成しました。
事例:コードの解説
#! /usr/bin/jython2.5b3 from java.awt import BorderLayout from java.awt.event import ActionListener from javax.swing import JButton from javax.swing import JFrame from javax.swing import JPanel from javax.swing import JScrollPane from javax.swing import JTextArea from javax.swing import JToolBar from javax.swing import ImageIcon ## ---------------------------------------- class ToolBarDemo(JPanel, ActionListener): def __init__(self, *args): super(self.__class__,self).__init__( layout = BorderLayout(), preferredSize = (450, 130), ) toolBar = JToolBar("Still draggable") self.addButtons(toolBar) self.textArea = JTextArea( rows = 5, columns = 30, editable = False, ) scrollPane = JScrollPane(self.textArea) self.add(toolBar, BorderLayout.PAGE_START) self.add(scrollPane, BorderLayout.CENTER) def addButtons(self, toolBar): s = [ ( "Back24", PreviousCommand, # first button "Back to previous something-or-other", "Previous", ), ( "Up24", UpCommand, # second button "Up to something-or-other", "Up", ), ( "Forward24", NextCommand, # third button "Forward to something-or-other", "Next", ), ] for imageName, command, toolTipText, altText in s: button = self.makeNavigationButton( imageName, command, toolTipText, altText) toolBar.add(button) def makeNavigationButton(self, imageName, command, toolTipText, altText ): imageName = "images/%s.gif"%imageName button = CommandButton( icon = ImageIcon(imageName, altText), toolTipText = toolTipText, actionPerformed = self.actionPerformed, command = command(), ) return button def actionPerformed(self, e): s = "If this were a real app,\n it would have " self.displayResult(s+e.source.command())
属性 .command は、呼び出し可能オブジェクト(PreviousCommand/UpCommand/NextCommand)を参照するので、演算子 () に呼応して、メソッド __call__ の本体に記述したコードの断片を実行します。
def displayResult(self, actionDescription): self.textArea.append(actionDescription + "\n") self.textArea.caretPosition = self.textArea.document.length ## ---------------------------------------- class CommandButton(JButton): # Command::Invoker pass ## ---------------------------------------- class Command: # Command::Command def __call__(self): raise NotImplementedError, self.__class__.__name__ class PreviousCommand(Command): # Command::ConcreteCommand def __call__(self): return "taken you to the previous." class UpCommand(Command): # Command::ConcreteCommand def __call__(self): return "taken you up one level to ." class NextCommand(Command): # Command::ConcreteCommand def __call__(self): return "taken you to the next ."
メソッド __call__ は、演算子 () に呼応して、呼び出し可能オブジェクトの動作を規定します。
## ---------------------------------------- def ex(*args): frame = JFrame( title = "ToolBarDemo", defaultCloseOperation = JFrame.EXIT_ON_CLOSE, ) frame.add(ToolBarDemo()) frame.pack() frame.visible = True ## ---------------------------------------- if __name__=='__main__': ex()
Tips
》作業中です《
Last updated♪09/05/27