Copyright

Given the following Java code, debug and fix. 1 // Lets user type keys 2 // Displays each key...

Question:

Given the following Java code, debug and fix.

1 // Lets user type keys

2 // Displays each key typed

3 import javax.swing.*;

4 import java.awt.event.*;

5 import java.awt.*;

6 public class DebugFifteen3 extends JFrame implements KeyListener

7 {

8 char key;

9 Container con = null;

10 GridLayout grid = new GridLayout(2, 1);

11 JLabel label = new JLabel("Key Typed:");

12 JTextArea textArea = new JTextArea(4, 25);

13 public DebugFifteen3()

14 {

15 setTitle("Debug Key Frame");

16 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

17 con.setLayout(grid);

18 con.add(textArea);

19 con.add(label);

20 addKeyListener(this);

21 textArea.addKeyListener(this);

22 }

23

24 @Override

25 public void keyTyped()

26 {

27 char c = e.getKeyChar();

28 label.setText ("Key Typed: " + c);

29 }

30

31 @Override

32 public void keyReleased(KeyEvent e)

33 {

34 }

35

36 public static void main(String[] args)

37 {

38 DebugFifteen3 kFrame = new DebugFifteen3();

39 kFrame.setSize(300, 200);

40 kFrame.setVisible(true);

41 }

42 }

43

44

45

Debugging in Java:

Debugging is the process of detecting and removing of existing and potential errors (also called as 'bugs') in a software code that can cause it to behave unexpectedly or crash. Description: To debug a program, user has to start with a problem, isolate the source code of the problem, and then fix it.

Answer and Explanation:

Coding:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class DebugFifteen3 extends JFrame implements KeyListener {
char key;
...

See full answer below.

Become a Study.com member to unlock this answer! Create your account

View this answer

Explore our homework questions and answers library