In the last tutorial we build a simple GUI where you had to place the control elements on the Frame. We had to give them the right size and location. One problem is that you have to try a litle bit until the sizes and locations of the control elements are fitting together, but the bigger problem is the resizeing of the frame. If you give the control elements a fixed position and size, they will always stay there. So the user can make the frame smaller and he will not see all of the control elements anymore. For this problem Java provides a solution the so called Layout Managers. This library provides three different Layout Managers. All three use the SimpleLayout structure.
So lets have a look how to use such a Layout Manager.
We have to program the following for this example:
#include "grubc/awt/frame.h" #include "grubc/awt/panel.h" #include "grubc/awt/label.h" #include "grubc/awt/textarea.h" #include "grubc/awt/checkbox.h" #include "grubc/awt/checkboxgroup.h" #include "grubc/awt/button.h" #include "grubc/swing/messagebox.h" #include "grubc/awt/simplelayout.h" /* Declare the needed variables. */ Jvm jvm; Frame frame; Panel panel; Label label; TextArea textarea; CheckBox cbask; CheckBox cbdirectly; CheckBoxGroup cbgroup; Button buttonexit; SimpleLayout borderlayout; SimpleLayout flowlayout; jboolean exitprg = JNI_FALSE; /* Declare the needed listener callback function. */ void windowListener(jint event, Window window); void actionListener(jobject Component); int main(int argc, char *argv[]) { WindowListener wl; /* WindowListener Object. */ ActionListener albuttonexit; /* ActionListener Object. */ initJvm(&jvm, "./grubc.jar"); /* Initialize the JVM. */ createFrame(&frame); /* Create the frame. */ createPanel(&panel); /* Create the panel. */ createLabel(&label); /* Create the label. */ createTextArea(&textarea); /* Create the textarea. */ createCheckBox(&cbask); /* Create the first checkbox. */ createCheckBox(&cbdirectly); /* Create the second checkbox. */ createCheckBoxGroup(&cbgroup); /* Create the checkboxgroup. */ createButton(&buttonexit); /* Create the exit button. */ createBorderLayout(&borderlayout, 0, 0); /* Create the borderlayout. */ createFlowLayout(&flowlayout, 10, 10, FLOWLAYOUT_RIGHT); /* Create the flowlayout. */ /* Register the callback functions as listeners. */ wl = frame.addWindowListener(&windowListener); albuttonexit = buttonexit.addActionListener(&actionListener); /* Set the title, state and layout of the frame. */ frame.setTitle("Tutorial 5"); frame.setSize(600, 300); frame.setLayout(borderlayout.getObject()); /* Set the panels layout. */ panel.setLayout(flowlayout.getObject()); /* Set the labels text. */ label.setText("Closing Options:"); /* Set the radiobuttons captions and group. */ cbask.setLabel("ask before closing"); cbask.setCheckBoxGroup(&cbgroup); cbask.setState(JNI_TRUE); cbdirectly.setLabel("close immediatly"); cbdirectly.setCheckBoxGroup(&cbgroup); /* Set the exit buttons text. */ buttonexit.setLabel("Exit"); /* Add the control elements to the frame and the panel. */ frame.addComponentWithConstraint(panel.getObject(), BORDERLAYOUT_NORTH); frame.addComponentWithConstraint(textarea.getObject(), BORDERLAYOUT_CENTER); panel.addComponent(label.getObject()); panel.addComponent(cbask.getObject()); panel.addComponent(cbdirectly.getObject()); panel.addComponent(buttonexit.getObject()); frame.show(); /* Display the frame. */ /* Wait until the user wants to close the frame (exitprg is true). */ while (!exitprg) { jnisleep(100); /* Sleep 100 ms. */ if (exitprg) { /* Should frame be closed??? */ if (cbask.getState()) { /* Ask before closing??? */ exitprg = (showModalConfirmDialog(&frame, "Do you really want to exit?") == MSGBOX_YES); } } } frame.close(); /* Close the frame. */ /* Deregister the callback functions. */ frame.removeWindowListener(wl); buttonexit.removeActionListener(albuttonexit); /* Cleanup */ deleteLabel(&label); deleteCheckBox(&cbask); deleteCheckBox(&cbdirectly); deleteCheckBoxGroup(&cbgroup); deleteButton(&buttonexit); deleteTextArea(&textarea); deleteSimpleLayout(&borderlayout); deleteSimpleLayout(&flowlayout); deletePanel(&panel); 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 exit button??? */ if (isSameObject(source, buttonexit.getObject())) { exitprg = JNI_TRUE; } }
When you execute it you will get a window which looks like this: