Page 206 - CTS - CSA TP - Volume 2
P. 206

COMPUTER SOFTWARE APPLICATION - CITS




           TASK 2: A simple graphical window (frame) with a label and Window Event Handling
              import java.awt.Frame;
              import java.awt.Label;



              // CustomFrame class extending Frame
              class CustomFrame extends Frame {
                  public CustomFrame(String title) {

                      super(title);
                  }
              }


              // Main class demonstrating the usage
              public class ContainerExample {

                  public static void main(String[] args) {
                      // Creating an instance of CustomFrame
                      CustomFrame customFrame = new CustomFrame(“Custom Container Example”);



                      // Adding a label to the custom frame
                      Label label = new Label(“Hello, this is a simple container!”);
                      customFrame.add(label);


                      // Setting size and visibility

                      customFrame.setSize(300, 200);
                      customFrame.setVisible(true);


                      // Adding window close event handling
                      customFrame.addWindowListener(new java.awt.event.WindowAdapter() {

                          public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                              System.exit(0);
                          }
                      });
              }}

           Output:
















                                                           191

                              CITS : IT & ITES - Computer Software Application - Exercise 116
   201   202   203   204   205   206   207   208   209   210   211