Sunday, December 22, 2013

Example of programming with java-gnome

Once installed libjava-gnome-java, we can prepare our "Hello World" using java-gnome. Basically it have the same function as "GTK+ exercise: GtkBox - A container box", a basic GTK+ Window with 3 Labels.

helloGnome
helloGnome
Edit helloGnome.java
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Window;
import org.gnome.gtk.Widget;
import org.gnome.gdk.Event;
import org.gnome.gtk.Label;
import org.gnome.gtk.HBox;

public class helloGnome
{
    public static void main(String[] args) {
        final Window window;
        final HBox hBox;
        final Label label1, label2, label3;

        Gtk.init(args);

        window = new Window();
        window.setTitle("Hello Raspberry Pi - java-gnome exercise");
        window.connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });

        label1 = new Label("Label 1");
        label2 = new Label("Label 2");
        label3 = new Label("Label 3");

        hBox = new HBox(false, 3);
        hBox.add(label1);
        hBox.add(label2);
        hBox.add(label3);

        window.add(hBox);
        window.showAll();

        Gtk.main();
    }
}


Compile it with the command:
$ javac -classpath /usr/share/java/gtk-4.1.jar helloGnome.java

and run it:
$ java -classpath /usr/share/java/gtk-4.1.jar:. helloGnome

No comments: