import java.awt.*; import javax.swing.*; public class Test { public static void main(String[] args) { Component c = new JButton("OK"); JFrame frame = new JFrame("My Frame"); frame.add(c); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }

Analyze the following code.


import java.awt.*;
import javax.swing.*;
public class Test  {
  public static void main(String[] args) {
    Component c = new JButton("OK");
    JFrame frame = new JFrame("My Frame");
    frame.add(c);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }


A. You cannot assign a JButton to a variable of java.awt.Component.
B. You can only add c to a container because c's type is Component.
C. You cannot add a Swing component directly to a JFrame using add(c) prior to JDK 1.4, but it is OK in JDK 1.5.
D. You cannot create a JFrame using new JFrame("My Frame").

The correct answer is C


Java

Learn More Multiple Choice Question :