Introduction To Java Programming Language

105606
Back to Introduction To Java Programming Language

Module 5: Introduction to AWT and Applets

  1. 1(g). Event handling in Java commonly follows the: (i) Delegation event model (ii) Sequential event model (iii) Inheritance event model (iv) Compilation event model20262m

    Module 5: Introduction to AWT and Applets

    Event handling in Java commonly follows the:

    (i) Delegation event model
    (ii) Sequential event model
    (iii) Inheritance event model
    (iv) Compilation event model

    View this question on its own page →
    Worked Solution

    Answer: (i) Delegation event model

    In Java GUI programming, an event source (such as a button) generates an event and delegates handling to registered listener objects. The listener implements an interface such as ActionListener.

    MermaidDiagram
    Rendering diagram…
  2. 1(i). AWT stands for: (i) Advanced Window Technology (ii) Application Window Tool (iii) Abstract Window Toolkit (iv) Abstract Web Toolkit20262m

    Module 5: Introduction to AWT and Applets

    AWT stands for:

    (i) Advanced Window Technology
    (ii) Application Window Tool
    (iii) Abstract Window Toolkit
    (iv) Abstract Web Toolkit

    View this question on its own page →
    Worked Solution

    Answer: (iii) Abstract Window Toolkit

    AWT is Java's Abstract Window Toolkit, a GUI library providing components such as Frame, Button, Label, and TextField. Many AWT components use native operating-system peers.

  3. 8(b). Construct an AWT application containing a frame, label, text field and button. Develop an event-handling mechanism that displays a message when the button is pressed.20267m

    Module 5: Introduction to AWT and Applets

    Construct an AWT application containing a frame, label, text field and button. Develop an event-handling mechanism that displays a message when the button is pressed.

    View this question on its own page →
    Worked Solution

    AWT event-handling example

    import java.awt.*;
    import java.awt.event.*;
    
    public class GreetingApp {
        public static void main(String[] args) {
            Frame frame = new Frame("Greeting");
            Label label = new Label("Enter your name:");
            TextField input = new TextField(18);
            Button button = new Button("Greet");
            Label output = new Label(" ");
            Panel panel = new Panel();
            panel.add(label); panel.add(input); panel.add(button); panel.add(output);
            button.addActionListener(e -> output.setText("Hello, " + input.getText()));
            frame.add(panel);
            frame.setSize(420, 120);
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) { frame.dispose(); }
            });
            frame.setVisible(true);
        }
    }
    

    The button is the event source. addActionListener registers a listener; a click generates an ActionEvent and invokes the handler. The window listener closes the frame cleanly.

  4. 9(d). Write a short note on Applets and Applet Parameters.20267m

    Module 5: Introduction to AWT and Applets

    Write a short note on Applets and Applet Parameters.

    View this question on its own page →
    Worked Solution

    Applets and applet parameters

    A Java applet was a small Java program designed to run inside an applet viewer or browser plugin. Applets historically used lifecycle methods such as init(), start(), stop() and destroy(). Modern browsers no longer support Java applets, so they are a legacy topic rather than a current deployment choice.

    Parameters could be passed in HTML using <param name="color" value="blue"> and read with getParameter("color").

    public void init() {
        String color = getParameter("color");
        if (color == null) color = "blue";
        // use the parameter in applet initialization
    }
    
    MermaidDiagram
    Rendering diagram…

    For modern applications, use standard desktop, web or mobile technologies instead.