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

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 JButton("OK"));
    frame.add(new JButton("Cancel"));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }


A. Only button OK is displayed.
B. Only button Cancel is displayed.
C. Both button OK and button Cancel are displayed and button OK is displayed on the left side of button OK.
D. Both button OK and button Cancel are displayed and button OK is displayed on the right side of button OK.

The correct answer is B

Explanation: By default, the layout of the content pane in a JFrame is BorderLayout. Button OK is placed in the center of content pane, then button Cancel is placed in the same place. So you only can see button Cancel.


Java

Learn More Multiple Choice Question :