[Previous] [Next] [Up] [Contents] [Feedback]

5. Beispiele, Applets?


Das übliche Hello-World Beispiel kann damit etwa folgendermaßen aussehen:




// don't use gtk.*
import gtk.Application;
import gtk.Label;
import gtk.RunLoop;
import gtk.Window;

public class Hello
{
    public static void main (String args[])
    {
	new Application(args);
	new Hello();
	RunLoop.defaultRunLoop().run();
    }

    public Hello ()
    {
	Window window = new Window(Window.TOPLEVEL);
	Label label = new Label("Hello, World!");

	try {
	    window.connect("destroy", this, "quit");
	} catch (NoSuchMethodException e) {
	    throw new RuntimeException(e.toString());
	}

	window.add(label);
	window.showAll();
    }

    public void quit (Object obj)
    {
	System.err.println("exiting now...");
	System.exit(0);
    }
}



Ein komplexeres Beispiel mit vielen Threads (einer pro Fenster):




// don't use gtk.*
import gtk.Adjustment;
import gtk.Application;
import gtk.Button;
import gtk.Entry;
import gtk.HBox;
import gtk.HScale;
import gtk.RunLoop;
import gtk.Tooltips;
import gtk.VBox;
import gtk.Window;

public class Simple extends Thread
{
    protected Entry e1;
    protected Entry e2;
    protected Adjustment a1;
    protected Adjustment a2;

    public static void main (String args[])
    {
	new Application(args);
	// new Foo().start();	// loop (busy wait)
	new Simple().start();
    }

    public void run ()
    {
	Window window = new Window(Window.TOPLEVEL);
	Tooltips tips = new Tooltips();
	HBox box = new HBox(true, 2);
	VBox box1 = new VBox(true, 2);
	VBox box2 = new VBox(true, 2);
	Button b1 = new Button("Create New Window");
	Button b2 = new Button("Quit Application");
	HScale s1, s2;

	e1 = new Entry();
	e2 = new Entry();
	a1 = new Adjustment(0, 0, 200, 1, 10, 0);
	a2 = new Adjustment(0, 0, 200, 1, 10, 0);
	s1 = new HScale(a1);
	s2 = new HScale(a2);

	tips.setTip(b1, "Button 1", null);
	tips.setTip(b2, "Button 2", null);

	try
	{
	    e1.connect("activate", this, "entry1");
	    e2.connect("activate", this, "entry2");
	    a1.connect("value_changed", this, "adjust1");
	    a2.connect("value_changed", this, "adjust2");
	    b1.connect("clicked", this, "click");
	    b2.connect("clicked", this, "quit");
	    window.connect("destroy", this, "close");
	}
	catch (NoSuchMethodException e)
	{
	    throw new RuntimeException(e.toString());
	}

	box1.add(s1);
	box1.add(e1);
	box1.add(b1);
	box2.add(s2);
	box2.add(e2);
	box2.add(b2);
	box.add(box1);
	box.add(box2);
	window.add(box);
	window.showAll();

	RunLoop.defaultRunLoop().run();
    }

    public void entry1 (Object obj)
    {
	Entry sender = (Entry) obj;

	a1.setValue(Float.valueOf(sender.getText()).floatValue());
    }

    public void entry2 (Object obj)
    {
	Entry sender = (Entry) obj;

	a2.setValue(Float.valueOf(sender.getText()).floatValue());
    }

    public void adjust1 (Object obj)
    {
	Adjustment sender = (Adjustment) obj;

	e1.setText(Float.toString(sender.getValue()));
	// a2.setValue(value * 2);
    }

    public void adjust2 (Object obj)
    {
	Adjustment sender = (Adjustment) obj;

	e2.setText(Float.toString(sender.getValue()));
	// a1.setValue(value / 2);
    }

    public void click (Object obj)
    {
	System.err.println("creating new thread...");
	new Simple().start();
    }

    public void quit (Object obj)
    {
	System.err.println("exiting now...");
	System.exit(0);
    }

    public void close (Object obj)
    {
	System.err.println("terminating thread...");
	RunLoop.defaultRunLoop().terminate();
    }
}



Dieses Beispiel kann man (wenn man die Permissions richtig setzt) dann auch im Applet-Viewer laufen lassen:




[Previous] [Next] [Up] [Contents] [Feedback]