~/home_Jython/berry_py/worldCup/ex02/wc02_JTextArea.py
|INDEX| Java.use(better, Jython)
》作業中です《
#! /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 """ >>> tips1() >>> #tips2() >>> ## ---------------------------------------- >>> None version: #1.0.20 """ from __init__ import * ## ---------------------------------------- ## 気軽に Swing〔2〕JTextArea: 各チーム紹介 ## Java.use(better,Swing=Jython) 〜 萬プログラマーのための Python 導入ガイド ## ---------------------------------------- ## ---------------------------------------- ## 2008/03/17《2.1》今回の課題:相手チームを知ろう ## ---------------------------------------- from os.path import dirname, exists from sys import argv from java.awt import BorderLayout from java.awt import Font 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 from javax.swing import JTextArea from javax.swing import ListSelectionModel class TeamsListPanel(JPanel): 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"}, ], } def __init__(self, master, *args, **keys): def teams(): return [e for group in "ABCDEFGH" for team in self.groups[group] for e in team] listPane = self._listPane( listData = teams(), minimumSize = (100,50), valueChanged = self, ) canvasPane = self._canvasPane( minimumSize = (100,50), ) master.contentPane = self._splitPane( leftComponent = listPane, rightComponent = canvasPane, ) self._update("rsa") ## ---------------------------------------- def _listPane(self, listData, minimumSize, **keys): def view(listData, **keys): comp = JList( listData, selectionMode = ListSelectionModel.SINGLE_SELECTION, selectedIndex = 0, **keys ) return comp comp = JScrollPane( viewportView = view(listData, **keys), minimumSize = minimumSize, ) return comp def _canvasPane(self, minimumSize, **keys): comp = JPanel( layout = BorderLayout(), ) comp.add( self._labelPane(), BorderLayout.NORTH, ) comp.add( self._textPane(minimumSize), BorderLayout.CENTER, ) return comp def _labelPane(self, **keys): def view(): self.label = \ comp = JLabel( horizontalAlignment = JLabel.CENTER, horizontalTextPosition = JLabel.CENTER, verticalTextPosition = JLabel.BOTTOM, ) comp.font = comp.font.deriveFont(Font.ITALIC) return comp comp = view() return comp def _textPane(self, minimumSize, **keys): def view(): self.textArea = \ comp = JTextArea( lineWrap = True, wrapStyleWord = True, ) comp.font = comp.font.deriveFont(Font.ITALIC) return comp comp = JScrollPane( viewportView = view(), minimumSize = minimumSize, ) return comp def _splitPane(self, **keys): comp = JSplitPane( orientation = JSplitPane.HORIZONTAL_SPLIT, oneTouchExpandable = True, dividerLocation = 120, **keys ) return comp ## ---------------------------------------- def __call__(self, e): # ListSelectionListener.valueChanged s = e.source.selectedValue self._update(s) def _update(self, s=None): def imageIcon(path): return ImageIcon(path) if exists(path) else None def name(key): for group in self.groups.values(): for team in group: if key in team: return team[key] else: return None def contents(path): if exists(path): return "".join([e for e in open(path)]) else: return None path = dirname(argv[-1]) self.label.icon = imageIcon("%s/squad/%s.gif"%(path, s)) s = name(s) self.label.text = s text = contents("%s/profile/%s.txt"%(path, s)) self.textArea.text = text if text else "*** File not found ***" self.textArea.caretPosition = 0 ## ---------------------------------------- def tips1(): frame = JFrame( title = u"2010 FIFA World Cup South Africa™", size = (340,200), defaultCloseOperation = JFrame.EXIT_ON_CLOSE, locationRelativeTo = None, ) TeamsListPanel(frame) frame.show() #=============================================================================== # 2008/03/18《2.2》Swing:TextArea #------------------------------------------------------------------------------ # javax.swing.JTextArea # public JTextArea(String text) # public void setLineWrap(boolean wrap) # public void setWrapStyleWord(boolean word) # javax.swing.text.JTextComponent # public void setText(String t) #=============================================================================== ex="ex_JTextArea" def ex_JTextArea(): text = `dir(JTextArea)` view = JTextArea( text=text, lineWrap=True, wrapStyleWord=True, ) frame = JFrame( defaultCloseOperation=JFrame.EXIT_ON_CLOSE, title=view.__class__.__name__, preferredSize=(220, 100), ) frame.add(JScrollPane(view)) frame.pack() frame.show() #=============================================================================== # 2008/03/19《2.3》テキストファイルの内容を表示する #------------------------------------------------------------------------------ # javax.swing.JTextArea # public void append(String str) # javax.swing.text.JTextComponent # public void setCaretPosition(int position) #=============================================================================== ex="ex_JTextArea_append" def ex_JTextArea_append(): view = JTextArea( lineWrap=True, wrapStyleWord=True, ) name = "Teams/ITA.txt" for e in file(name).readlines(): view.append(e) view.caretPosition = 0 frame = JFrame( defaultCloseOperation=JFrame.EXIT_ON_CLOSE, title=name, preferredSize=(220, 100), ) frame.add(JScrollPane(view)) frame.pack() frame.show() #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ #=============================================================================== # 2007/06/26�s47�tJTextArea�i2�j�I�Ⳃꂽ�炱���Ȃ�́� #------------------------------------------------------------------------------ # javax.swing.JTextArea # public void setLineWrap(boolean wrap) # public void setWrapStyleWord(boolean word) #=============================================================================== #ex="ex_JTextArea_wrapStyle" def ex_JTextArea_wrapStyle(): b = False, True wrap = [(x, y) for x in b for y in b] text = reduce(lambda s, e: s+e+" ", dir(JTextArea), "") x, y, width, height = 10, 0, 200, 100 for i, (line, word) in enumerate(wrap): view = JTextArea( text=text, lineWrap=line, wrapStyleWord=word, ) frame = JFrame( defaultCloseOperation=JFrame.EXIT_ON_CLOSE, title="line=%s, word=%r"%(line, word), bounds=((x+width)*i, y, width, height), ) frame.add(JScrollPane(view)) frame.show() #=============================================================================== # #=============================================================================== #ex="ex_JLabel_HTML" def ex_JLabel_HTML(): text = """<HTML> <A href="/06/en/060622/1/82cv.html"><H2>Angola out but not down</H2></A> <SPAN>A combination of inexperience and tactical limitations were Angola’s undoing at the 2006 FIFA World Cup. Their departure after the group stage was not entirely unexpected but they can be pleased with their overall performance... <A href="/06/en/060622/1/82cv.html">More...</A></SPAN> <HTML>""" view = JLabel( text=text, ) frame = JFrame( defaultCloseOperation=JFrame.EXIT_ON_CLOSE, title="FIFA World Cup", bounds=(0, 0, 200, 100), ) frame.add(JScrollPane(view)) frame.visible = True #=============================================================================== # 2007/06/26�s47�tJTextArea�i2�j�I�Ⳃꂽ�炱���Ȃ�́� #=============================================================================== from random import * def exSw_JTextArea_actionPerformed(): def action(e): view.append("item #%2d\n"%(random()*100)) view = JTextArea() button = JButton(text="action", actionPerformed=action) frame = JFrame(defaultCloseOperation=JFrame.EXIT_ON_CLOSE, title="JTextArea", bounds=(0, 0, 180, 100)) frame.add(JScrollPane(view), BorderLayout.CENTER) frame.add(button, BorderLayout.EAST) frame.visible = True def exSw_JTextArea_eval(): def action(e): eval(compile(view.text, "<input>", "exec")) text = """from javax.swing import * JFrame(size=(100,80),visible=True) """ view = JTextArea(text=text) button = JButton(text="eval", actionPerformed=action) frame = JFrame(defaultCloseOperation=JFrame.EXIT_ON_CLOSE, title="Simple Python Environment", bounds=(0, 0, 300, 100)) frame.add(JScrollPane(view), BorderLayout.CENTER) frame.add(button, BorderLayout.EAST) frame.visible = True #=============================================================================== # 2007/06/27�s48�tJTextField �Ƃ��̒��Ԃ����i1�j�� #=============================================================================== def exSw_TextControls(): def action_JTextField(e): view.text = e.source.text def action_JFormattedTextField(e): view.text = e.source.value def action_JPasswordField(e): view.text = `e.source.password` panel = JPanel( componentOrientation=ComponentOrientation.LEFT_TO_RIGHT, layout=GridLayout(3, 2)) for e in JTextField, JFormattedTextField, JPasswordField: c = e.__name__.split(".")[-1] panel.add(JLabel(text=c)) panel.add(eval("%s(actionPerformed=action_%s)"%(c, c))) view = JTextArea() frame = JFrame(defaultCloseOperation=JFrame.EXIT_ON_CLOSE, title="Text Controls", bounds=(0, 0, 260, 110)) frame.add(panel, BorderLayout.NORTH) frame.add(view, BorderLayout.CENTER) frame.visible = True #=============================================================================== # 2007/06/28�s49�tJTextField �Ƃ��̒��Ԃ����i2�j�� #=============================================================================== # javax.swing.JFormattedTextField # public JFormattedTextField() # public JFormattedTextField(Format format) # public JFormattedTextField.AbstractFormatter getFormatter() # public Object getValue() #=============================================================================== from java.util import * def exSw_JFormattedTextField(): def action(e): print "formatter:", view.formatter print "value :", view.value view = JFormattedTextField( value=Date(), actionPerformed=action) print "formatter:", view.formatter print "value :", view.value frame = JFrame(defaultCloseOperation=JFrame.EXIT_ON_CLOSE, title="JFormattedTextField", bounds=(0, 0, 210, 50)) frame.add(JScrollPane(view)) frame.visible = True ## ---------------------------------------- from doctest import testmod if __name__=='__main__': inform() testmod() ## ========================================