Analyze the following code.
import java.awt.*;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("My Frame");
frame.add(new MyDrawing("Welcome to Java!"));
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setVisible(true);
}
}
class MyDrawing extends JPanel {
String message;
public MyDrawing(String message) {
this.message = message;
}
public void paintcomponent(Graphics g) {
super.paintComponent(g);
g.drawString(message, 20 ,20);
}
}
A. The program runs fine and displays Welcome to Java!
B. The program has a compile error because the paintcomponent should be spelled as paintComponent.C. The program has a runtime error because the paintcomponent should be spelled as paintComponent.
D. The program runs, but it does not display the message.
E. It is a runtime error to invoke the setVisible(true) twice.
The correct answer is D
Explanation: The program runs, but it does not display the message, because the paintComponent method is not overridden. You have to spell paintComponent not paintcomponent.