We have seen how to draw on the frame, but now we will create a simple GUI. We will place a Label, a Textfield and two Buttons on the Frame. When you click the first Button we will display a Messagebox with the text of the Textfield. The second Button will close the frame.
We have to program the following for this example:
#include "grubc/awt/frame.h" #include "grubc/awt/label.h" #include "grubc/awt/textfield.h" #include "grubc/awt/button.h" #include "grubc/swing/messagebox.h" /* Declare the needed variables. */ Jvm jvm; Frame frame; Label label; TextField textfield; Button buttonmsgbox; Button buttonexit; jboolean exitprg = JNI_FALSE; /* Declare the needed listener callback function. */ void windowListener(jint event, Window window); void actionListener(Component source); int main(int argc, char *argv[]) { WindowListener wl; /* WindowListener Object. */ ActionListener albuttonmsgbox; /* ActionListener Object. */ ActionListener albuttonexit; /* ActionListener Object. */ initJvm(&jvm, "./grubc.jar"); /* Initialize the JVM. */ createFrame(&frame); /* Create the frame. */ createLabel(&label); /* Create the label. */ createTextField(&textfield); /* Create the textfield. */ createButton(&buttonmsgbox); /* Create the first button. */ createButton(&buttonexit); /* Create the second button. */ /* Register the callback functions as listeners. */ wl = frame.addWindowListener(&windowListener); albuttonmsgbox = buttonmsgbox.addActionListener(&actionListener); albuttonexit = buttonexit.addActionListener(&actionListener); /* Set the title and state of the frame. */ frame.setTitle("Tutorial 4"); frame.setSize(600, 300); /* Set the labels text, position and size. */ label.setText("Text:"); label.setSize(30, 10); label.setLocation(50, 50); /* Set the textfields size and location. */ textfield.setSize(250, 25); textfield.setLocation(80, 45); /* Set the first buttons text, size and location. */ buttonmsgbox.setLabel("MessageBox"); buttonmsgbox.setSize(100, 25); buttonmsgbox.setLocation(350, 40); /* Set the second buttons text, size and location. */ buttonexit.setLabel("Exit"); buttonexit.setSize(50, 25); buttonexit.setLocation(450, 40); /* Add the control elements to the frame. */ frame.addComponent(label.getObject()); frame.addComponent(textfield.getObject()); frame.addComponent(buttonmsgbox.getObject()); frame.addComponent(buttonexit.getObject()); frame.show(); /* Display the frame. */ /* Wait until exitprg is true. */ WAIT_UNTIL_TRUE(exitprg); frame.close(); /* Close the frame. */ /* Deregister the callback functions. */ frame.removeWindowListener(wl); buttonmsgbox.removeActionListener(albuttonmsgbox); buttonexit.removeActionListener(albuttonexit); /* Cleanup */ deleteLabel(&label); deleteButton(&buttonmsgbox); deleteButton(&buttonexit); deleteTextField(&textfield); deleteFrame(&frame); return 0; } void windowListener(jint event, Window window) { if (frame.isSameWindow(window)) { switch (event) { case CB_WINDOW_OPENED: break; case CB_WINDOW_CLOSING: exitprg = JNI_TRUE; /* Frame should be closed. */ break; case CB_WINDOW_CLOSED: break; case CB_WINDOW_ICONIFIED: break; case CB_WINDOW_DEICONIFIED: break; case CB_WINDOW_ACTIVATED: break; case CB_WINDOW_DEACTIVATED: break; } } } void actionListener(Component source) { /* Check if the source was the messagebox button or the exit button??? */ if (isSameObject(source, buttonmsgbox.getObject())) { char *text = textfield.getText(); /* Get the text of the Textfield. */ showModalMessageDialog(&frame, text); /* Show the Messagebox. */ free(text); /* Free the text. */ } else if (isSameObject(source, buttonexit.getObject())) { exitprg = JNI_TRUE; } }
When you execute it you will get a window which looks like this: