next up previous
Nächste Seite: Über dieses Dokument ... Aufwärts: JPython Vorherige Seite: JPython

Applets in JPython

from java.applet import Applet
from java.awt import *
from java.awt.event import ActionListener

import pawt

class HelloWorldApplet(Applet, ActionListener):
    def init(self):
        self.button = Button("Klick")
        self.button.addActionListener(self)
        self.add(self.button)
        self.textfield = TextField(20)
        self.add(self.textfield)
        self.textfield.setEditable(0)
        
    def actionPerformed(self, e):
        if (e.getActionCommand() == "Klick"):
            self.textfield.setText("Hello World")
            
if __name__ == "__main__":
    pawt.test(HelloWorldApplet())

Um ein Skript applet-fähig zu machen muß ein Java-Archiv erstellt werden:

Sourcecode BrowseApplet.py:
from java.applet import Applet
from java.awt import *
from java.awt.event import ActionListener, ItemListener

import pawt

class BrowseApplet(Applet, ActionListener, ItemListener):
    "Namespace Browser-Applet in JPython"
    import sys
    def init(self):
        self.classNamespace = BrowseApplet

        self.backward = Button("zurueck")
        self.backward.addActionListener(self)
        self.add(self.backward)
        self.backward.setEnabled(0)

        self.list = List(6, 0)
        self.list.addItemListener(self)
        self.list.addActionListener(self)
        self.add(self.list)
        
        
        self.textarea = TextArea("", 10, 40, TextArea.SCROLLBARS_VERTICAL_ONLY)
        self.add(self.textarea)
        self.textarea.setEditable(0)
        
        
        self.object = self
        self.displayNamespace()
        self.history = []
        

    def displayNamespace(self):
        name = dir(self.object)
        self.list.removeAll()
        for i in name:
            self.list.add(i)
    def itemStateChanged(self, i):
        item = self.list.getSelectedItem()
        if item:
            self.textarea.setText(str(getattr(self.object, item)))
        
    def actionPerformed(self, e):
        if (e.getActionCommand() == "zurueck"):
            if self.history:
                self.object = self.history.pop()
                self.displayNamespace()
            if not self.history:
                self.backward.setEnabled(0)
        else:                           # Doppelklick auf liste
            item = self.list.getSelectedItem()
            self.history.append(self.object)
            self.object = getattr(self.object, item)
            self.displayNamespace()
            self.backward.setEnabled(1)
if __name__ == "__main__":
    pawt.test(BrowseApplet())



Juergen Hoetzel 2000-07-04