Question: Identify and Correct the following Programming Errors: The following program displays two buttons, OK and Cancel, in the window. A message is displayed on the
Identify and Correct the following Programming Errors:
The following program displays two buttons, OK and Cancel, in the window. A message is displayed on the console to indicate which button is clicked. Find and correct few mistakes in this program. Please list the line number and show how to correct mistakes.
-------------------------------------------------------------------------------------------------------------------------------------------
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4
5 public class Test extends JFrame implements ActionListener
6 {
7 /**Default constructor*/
8 public TestActionEvent() {
9 // Create two buttons
10 JButton jbtOk = new JButton("OK");
11 JButton jbtCancel = new JButton("Cancel");
12
13 // Set the window title
14 super("TestActionEvent");
15
16 // Set FlowLayout manager to arrange the components
17 // inside the frame
18 setLayout(new FlowLayout());
19
20 // Add buttons to the frame
21 add(jbtOk);
22 add(jbtCancel);
23 setSize(100, 80);
24 setVisible(true);
25 } //end constructor
26
27 /**Main method*/
28 public static void main(String[ ] args) {
29 TestActionEvent frame = new TestActionEvent();
30 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
31 }//end main method
32
33 /**This method will be invoked when a button is clicked*/
34 public void actionPerformed() {
35 if (event.getSource() == jbtOk) {
36 System.out.println("The OK button is clicked");
37 }
38 else if (event.getSource() == jbtCancel) {
39 System.out.println("The Cancel button is clicked");
40 }
41 }//end actionPerformed method
42 }//end class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
