Important Web Design and Development Short Questions
Chapter: 12
Question 1:
How can you handle window closing events in Java?
Answer:
To handle window closing events in Java, you can implement the WindowListener interface and override the windowClosing(WindowEvent we) method to provide custom behavior, such as displaying a message before exiting.
Chapter: 12
Question 2:
What method of WindowListener is used to handle when a window is closing?
Answer:
The windowClosing(WindowEvent we) method is used to handle the event when a window is closing. This allows for custom actions to be performed, such as showing a confirmation message.
Chapter: 12
Question 3:
How does the EventsEx class in the window exit example handle both mouse and window events?
Answer:
The EventsEx class handles both mouse and window events by implementing MouseMotionListener for mouse events and WindowListener for window events. It registers both listeners with the JFrame.
Chapter: 12
Question 4:
What is the role of frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) in Java Swing?
Answer:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ensures that the application exits when the window is closed, returning control to the command prompt or terminating the program.
Chapter: 12
Question 5:
What does the initGUI() method do in the provided examples?
Answer:
The initGUI() method sets up the graphical user interface by creating and configuring the JFrame, adding components like labels and buttons, and registering event listeners for handling user interactions.
Chapter: 12
Question 6:
How is the windowClosing method used to display a message before closing the window?
Answer:
By overriding the windowClosing(WindowEvent we) method in the WindowListener interface, you can display a message dialog before the window closes, allowing users to see a custom farewell message.
Chapter: 13
Question 7:
Why do you need to provide implementations for all methods of an interface when only one method is used?
Answer:
When implementing an interface, all methods must be defined because the Java language requires a concrete implementation of all methods declared in the interface.
Chapter: 13
Question 8:
What is the purpose of using adapter classes in Java?
Answer:
Adapter classes provide default (empty) implementations for all methods of their corresponding listener interfaces, allowing you to override only the methods you are interested in.
Chapter: 13
Question 9:
Name two adapter classes provided by JDK for handling events.
Answer:
Two adapter classes provided by JDK are WindowAdapter for window events and MouseMotionAdapter for mouse motion events.
Chapter: 13
Question 10:
How does using WindowAdapter simplify event handling compared to using WindowListener directly?
Answer:
Using WindowAdapter simplifies event handling because it provides default (empty) implementations for all methods of WindowListener, so you only need to override the specific methods you are interested in.