tabbedPaneDemo/jython/TabbedPaneDemo.py

  1: #! /usr/bin/env python
  2: # coding: utf-8
  3: ## ----------------------------------------
  4: ##
  5: ## (C) Copyright 2000-2010, 小粒ちゃん《監修》小泉ひよ子とタマゴ倶楽部
  6: ##
  7: ## ----------------------------------------
  8: #...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
  9: """
 10: >>> tips()
 11: 
 12: >>> ## ----------------------------------------
 13: >>> None
 14: version: #1.0.24
 15: """
 16: ## ---------------------------------------- demo: jython
 17: """
 18: _jython=/Users/sketch/book/java_useBetter_1/_jython
 19: 
 20: cd /Users/sketch/book/java_useBetter_1/swing/JTabbedPane/tabbedPaneDemo/jython
 21: 
 22: JYTHONPATH=$_jython jython -i TabbedPaneDemo.py
 23: 
 24: ============================================================
 25: Version: 2.5.0 (Release_2_5_0:6476, Jun 16 2009, 13:33:26) 
 26: Module : ButtonDemo.py
 27: Date   : Tue Sep 14 06:44:24 2010
 28: ============================================================
 29: """
 30: ## ----------------------------------------
 31: ##package components;
 32: 
 33: from java.awt import GridLayout
 34: from java.awt.event import KeyEvent
 35: from java.lang import System
 36: from javax.swing import ImageIcon
 37: from javax.swing import JFrame
 38: from javax.swing import JLabel
 39: from javax.swing import JPanel
 40: from javax.swing import JTabbedPane
 41: 
 42: from os.path import exists
 43: 
 44: ## ----------------------------------------
 45: class TabbedPaneDemo(JPanel):
 46:     
 47:     def __init__(self, master):
 48:         super(JPanel,self).__init__(GridLayout(1, 1))
 49:         master.add(self)
 50: 
 51:         tabbedPane = JTabbedPane(
 52:             #/The following line enables to use scrolling tabs.
 53:             tabLayoutPolicy = JTabbedPane.SCROLL_TAB_LAYOUT,
 54:             )
 55:         icon = self.createImageIcon("_images/middle.gif")
 56: 
 57:         class makeTextPanel(JPanel):
 58:             def __init__(self, text, **keys):
 59:                 super(JPanel,self).__init__(GridLayout(1, 1), **keys)
 60:                 filler = JLabel(
 61:                     text = text,
 62:                     horizontalAlignment = JLabel.CENTER,
 63:                     )
 64:                 self.add(filler)
 65: 
 66:         panel = makeTextPanel("Panel #1")
 67:         tabbedPane.addTab(
 68:             "Tab 1", icon, panel, "Does nothing")
 69:         tabbedPane.setMnemonicAt(0, KeyEvent.VK_1)
 70: 
 71:         panel = makeTextPanel("Panel #2")
 72:         tabbedPane.addTab(
 73:             "Tab 2", icon, panel, "Does twice as much nothing")
 74:         tabbedPane.setMnemonicAt(1, KeyEvent.VK_2)
 75: 
 76:         panel = makeTextPanel("Panel #3")
 77:         tabbedPane.addTab(
 78:             "Tab 3", icon, panel, "Still does nothing")
 79:         tabbedPane.setMnemonicAt(2, KeyEvent.VK_3)
 80: 
 81:         panel = makeTextPanel(
 82:             "Panel #4 (has a preferred size of 410 x 50).",
 83:             preferredSize = (410, 50),
 84:             )
 85:         tabbedPane.addTab(
 86:             "Tab 4", icon, panel, "Does nothing at all")
 87:         tabbedPane.setMnemonicAt(3, KeyEvent.VK_4)
 88: 
 89:         #/Add the tabbed pane to this panel.
 90:         self.add(tabbedPane)
 91: 
 92:     #/** Returns an ImageIcon, or null if the path was invalid. */
 93:     def createImageIcon(self, path):
 94:         imgURL = path if exists(path) else None
 95:         if imgURL != None:
 96:             return ImageIcon(imgURL)
 97:         else:
 98:             System.err.println("Couldn't find file: %s"%path)
 99:             return None
100: 
101:     ## ----------------------------------------
102:     """
103:      * Create the GUI and show it.  For thread safety,
104:      * this method should be invoked from
105:      * the event dispatch thread.
106:     """
107:     @staticmethod
108:     def createAndShowGUI():
109:         #/Create and set up the window.
110:         frame = JFrame(
111:             title = "TabbedPaneDemo",
112:             defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
113:             locationRelativeTo = None,
114:             )
115:         #/Add content to the window.
116:         TabbedPaneDemo(frame)
117: 
118:         #/Display the window.
119:         frame.pack()
120:         frame.visible = True
121: 
122: ## ----------------------------------------
123: from __init__ import *
124: tips = TabbedPaneDemo.createAndShowGUI
125: 
126: if __name__=='__main__':
127:     inform()
128:     testmod()
129: 
130: ## ========================================