In order to add a component to a container, first create an instance of the desired component and then call the add () method of the Container class to add it to a window. The add() method has many forms and one of these is,
Component add (Component c)
This method adds an instance of Component (i.e. c) to the container. The component added is automatically visible whenever its parent window is displayed.
import javax.swing.*;
class AddComponentJFrame
{
public static void main(String args[])
{
JFrame frame = new JFrame("Add Components to JFrame Java Example");
JButton button = new JButton("OK");
frame.add(button);
frame.setBounds(200,100,300,250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}