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

COMPUTER SOFTWARE APPLICATION - CITS




















           In this example, we create a custom class CustomFrame that extends the Frame class. We then create an
           instance of this custom class, add a Label component to it, and set the size and visibility. Additionally, a window
           close event listener is added to handle the closing of the frame.


           TASK 3: Button Example
              // importing Java AWT class

              import java.awt.*;


              // extending Frame class to our class ButtonExample
              public class ButtonExample extends Frame {


                  // initializing using constructor

                  ButtonExample() {


                      // creating a button
                      Button b = new Button(“Click Me!!”);



                      // setting button position on screen
                     b.setBounds(30, 100, 80, 30);


                      // adding button into frame

                      add(b);


                      // frame size 300 width and 300 height
                      setSize(300, 300);



                      // setting the title of Frame
                      setTitle(“This is our basic AWT example”);


                      // no layout manager
                      setLayout(null);







                                                           192

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