余録:worldCup/jython/ex11/wcFrame.py
#! /usr/bin/env python # coding: utf-8 ## ---------------------------------------- ## ## (C) Copyright 2000-2010, 小粒ちゃん《監修》小泉ひよ子とタマゴ倶楽部 ## ## ---------------------------------------- ## History: Swing Example "2010 FIFA World Cup South Africa™" ## 2003/07, Java/Jython ## 2006/07, Jython ## 2008/02, Jython 2.2.1 ## 2010/06, Jython 2.5.0 #...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 """ >>> tips() >>> ## ---------------------------------------- >>> None version: #1.0.15abc """ from __init__ import * ## ---------------------------------------- demo: jython """ $ cd /Users/sketch/home_sketch/worldCup/jython/ $ jython -i ex11/wcFrame.py """ ## ---------------------------------------- from wcModel import WcModel from javax.swing import ImageIcon from javax.swing import JFrame from javax.swing import JLabel from javax.swing import JList from javax.swing import JPanel from javax.swing import JScrollPane from javax.swing import JSplitPane class TopPanel(JPanel): def __init__(self, master, *args, **keys): master.contentPane = self def leftComponent(): listData = WcModel.teams view = JList( listData, # java.util.Vector valueChanged = self, ) comp = JScrollPane( viewportView = view, preferredSize = (120,150), ) return comp def rightComponent(): self.label = \ view = JLabel( icon = ImageIcon("matches/wc2010logo.png"), text = u"2010 FIFA World Cup South Africa™", horizontalAlignment = JLabel.CENTER, verticalTextPosition = JLabel.BOTTOM, horizontalTextPosition = JLabel.CENTER, ) comp = JScrollPane( viewportView = view, preferredSize = (180,0), ) return comp def splitPane(): comp = JSplitPane( orientation = JSplitPane.HORIZONTAL_SPLIT, oneTouchExpandable = True, dividerLocation = 70, leftComponent = leftComponent(), rightComponent = rightComponent(), ) return comp comp = splitPane() self.add(comp) # java.awt.Component master.pack() def __call__(self, e): # javax.swing.event.ListSelectionEvent value = e.source.selectedValue self.update_(value) def update_(self, team): self.label.icon = ImageIcon("squad/%s.gif"%team) self.label.text = WcModel.teamNames[team] ## ---------------------------------------- def tips(): frame = JFrame( title = "FIFA World Cup #11", locationRelativeTo = None, defaultCloseOperation = JFrame.EXIT_ON_CLOSE, ) TopPanel(frame) frame.visible = True ## ---------------------------------------- if __name__=='__main__': inform() testmod() ## ========================================
Java の素描 #011: 値を更新して再表示する
‖記事一覧‖ Java.use(better, Swing=Jython) #FIFA World Cup への道《Jython2.5》
Java の素描 〜 Jython 弾丸ツアー:時短プログラミング生活のすすめ 〜
《著》森こねこ、伊藤うさぎ、小粒ちゃん@湘南組《監修》タマゴ倶楽部
第1版♪2003/05/23 ● 第2版♪2006/04/03 ● 第3版♪2010/06/11
|関連記事|
step11: 値を更新して再表示する
。
リスト項目を選択したときに、ラベルを更新します。すると、ラベルを構成するテキストとアイコン画像が変化します。
class TopPanel(JPanel): def __init__(self, master, *args, **keys): ... def rightComponent(): self.label = \ view = JLabel( icon = ImageIcon("matches/wc2010logo.png"), text = u"2010 FIFA World Cup South Africa™", horizontalAlignment = JLabel.CENTER, verticalTextPosition = JLabel.BOTTOM, horizontalTextPosition = JLabel.CENTER, ) ... def __call__(self, e): # javax.swing.event.ListSelectionEvent value = e.source.selectedValue self.update_(value) def update_(self, team): self.label.icon = ImageIcon("squad/%s.gif"%team) self.label.text = WcModel.teamNames[team]
メソッド update_ では、選択したリスト項目の値 .selectedValue(実引数)によって確定する、各チーム team(仮引数)別の正式名称や旗を表示させます。
フォルダー squad には、各チームの旗を示す画像ファイルが、
"squad/正式名称.gif"
という名前で保存されているので、これを利用します。
リスト項目から jpn を選択すると、日本チームの旗を示すアイコン画像と、正式名称を示すテキスト "Japan" が表示されます。
》作業中です《
余録:worldCup/jython/ex11/wcModel.py
#! /usr/bin/env python # coding: utf-8 ## ---------------------------------------- ## ## (C) Copyright 2000-2010, 小粒ちゃん《監修》小泉ひよ子とタマゴ倶楽部 ## ## ---------------------------------------- ## History: Swing Example "2010 FIFA World Cup South Africa™" ## 2003/07, Java/Jython ## 2006/07, Jython ## 2008/02, Jython 2.2.1 ## 2010/06, Jython 2.5.0 #...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 """ >>> tips() >>> ## ---------------------------------------- >>> None version: #1.0.14 """ print(__name__,"1.0.02") ## ---------------------------------------- class WcModel: groups = { "A": [ # Group A {"rsa": "South Africa"}, {"mex": "Mexico"}, {"uru": "Uruguay"}, {"fra": "France"}, ], "B": [ # Group B {"arg": "Argentina"}, {"nga": "Nigeria"}, {"kor": "Korea Republic"}, {"gre": "Greece"}, ], "C": [ # Group C {"eng": "England"}, {"usa": "USA"}, {"alg": "Algeria"}, {"svn": "Slovenia"}, ], "D": [ # Group D {"ger": "Germany"}, {"aus": "Australia"}, {"srb": "Serbia"}, {"gha": "Ghana"}, ], "E": [ # Group E {"ned": "Netherlands"}, {"den": "Denmark"}, {"jpn": "Japan"}, {"cmr": "Cameroon"}, ], "F": [ # Group F {"ita": "Italy"}, {"par": "Paraguay"}, {"nzl": "New Zealand"}, {"svk": "Slovakia"}, ], "G": [ # Group G {"bra": "Brazil"}, {"prk": "Korea DPR"}, {"civ": "Cote d Ivoire"}, {"por": "Portugal"}, ], "H": [ # Group H {"esp": "Spain"}, {"sui": "Switzerland"}, {"hon": "Honduras"}, {"chi": "Chile"}, ], } teams = sorted([team for group in "ABCDEFGH" for teams in groups[group] for team in teams]) teamNames = dict*1 ## ========================================
*1:k,v) for group in groups.values() for team in group for k,v in team.items(