《Jython2.5》The Swing Tutorial, ComboBoxDemoProject

記事一覧

The Swing Tutorial《Jython2.5.0》
《事例》 ComboBoxDemoProject

《著》小粒ちゃん《監修》小泉ひよ子とタマゴ倶楽部
第1版♪2003/05/23 ● 第2版♪2009/04/03

概要

Swing によるオブジェクト指向プログラミングへの扉を開きます。
※ 公式サイト《The Java™ Tutorials》で公開されている例題を、Jython で再構成しました。

タマゴ倶楽部のスタッフが作成した例題(サンプル集)を公開します。原則として、オリジナル版の趣旨を尊重しつつ、Jython の特徴を活かすように改編しています。なお、プログラムの不具合については「小粒ちゃん」にお問い合わせください。


事例:モジュールの起動

$ jython -i ComboBoxDemo.py 
>>> tips()


事例:コードの解説

#! /usr/bin/env python
# coding: utf-8

## ----------------------------------------
##package components;

from java.awt import BorderLayout
from java.awt import Dimension
from java.awt import Font
from java.awt.event import ActionListener
from javax.swing import BorderFactory
from javax.swing import ImageIcon
from javax.swing import JComboBox
from javax.swing import JFrame
from javax.swing import JLabel
from javax.swing import JPanel

## ----------------------------------------
"""
 * ComboBoxDemo.java uses these additional files:
 *   images/Bird.gif
 *   images/Cat.gif
 *   images/Dog.gif
 *   images/Rabbit.gif
 *   images/Pig.gif
"""
class ComboBoxDemo(JPanel, ActionListener):

    def __init__(self):
        super(self.__class__,self).__init__(BorderLayout())

        petStrings = [ "Bird", "Cat", "Dog", "Rabbit", "Pig" ]

        ##Create the combo box, select the item at index 4.
        ##Indices start at 0, so 4 specifies the pig.
        petList = JComboBox(
            petStrings,
            selectedIndex = 4,
            )
        petList.addActionListener(self)

        ##Set up the picture.
        self.picture = JLabel(
            horizontalAlignment = JLabel.CENTER,
            border = BorderFactory.createEmptyBorder(10,0,0,0),
            )
        self.picture.font = self.picture.font.deriveFont(Font.ITALIC)
        self.updateLabel(petStrings[petList.selectedIndex])

        ##The preferred size is hard-coded to be the width of the
        ##widest image and the height of the tallest image + the border.
        ##A real program would compute this.
        self.picture.preferredSize = 177, 122+10

        ##Lay out the demo.
        self.add(petList, BorderLayout.PAGE_START)
        self.add(self.picture, BorderLayout.PAGE_END)
        self.border = BorderFactory.createEmptyBorder(20,20,20,20)

    ##* Listens to the combo box. */
    def actionPerformed(self, e):
        cb = e.source
        petName = cb.selectedItem
        self.updateLabel(petName)

    def updateLabel(self, name):
        icon = self.createImageIcon("_images/" + name + ".gif")
        self.picture.icon = icon
        self.picture.toolTipText = "A drawing of a " + name.lower()
        if icon != None:
            self.picture.text = None
        else:
            self.picture.text = "Image not found"

    ##* Returns an ImageIcon, or null if the path was invalid. */
    @staticmethod
    def createImageIcon(path):
        imgURL = path
        if imgURL != None:
            return ImageIcon(imgURL)
        else:
            System.err.println("Couldn't find file: " + path)
            return None

    """
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
    """
    @staticmethod
    def createAndShowGUI():
        ##Create and set up the window.
        frame = JFrame("ComboBoxDemo")
        frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE

        ##Create and set up the content pane.
        newContentPane = ComboBoxDemo()
        newContentPane.opaque = True ##content panes must be opaque
        frame.contentPane = newContentPane

        ##Display the window.
        frame.pack()
        frame.visible = True

def tips():
    ##Schedule a job for the event-dispatching thread:
    ##creating and showing this application's GUI.
    ComboBoxDemo.createAndShowGUI()

》こちらに移動中です《
TOP


関連記事

  • INDEX《Swing》JComboBox

Last updated♪2009/10/18