The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > JFrame, JWindow, JDialog  [6 examples]

e733. Creating a JFrame

A frame is a component container that displays its contents in a top-level window with a title bar and buttons to resize, iconify, maximize, and close the frame.

Unlike most Swing containers, adding a component to a frame is not done with the JFrame.add() method. This is because the frame holds several panes and it is necessary to specify a particular pane to which to add the component. The pane that holds child components is called the content pane. This example adds a text area to the content pane of a frame.

See also e559 Creating a Frame.

    // Create the frame
    String title = "Frame Title";
    JFrame frame = new JFrame(title);
    
    // Create a component to add to the frame
    JComponent comp = new JTextArea();
    
    // Add the component to the frame's content pane;
    // by default, the content pane has a border layout
    frame.getContentPane().add(comp, BorderLayout.CENTER);
    
    // Show the frame
    int width = 300;
    int height = 300;
    frame.setSize(width, height);
    frame.setVisible(true);

 Related Examples
e734. Exiting an Application When a JFrame Is Closed
e735. Disabling the Close Button on a JFrame
e736. Creating a Borderless Window
e737. Showing a Dialog Box
e738. Getting the JFrame of a Component

See also: Actions    JButton    JCheckBox    JComboBox    JDesktop and JInternalFrame    JLabel    JList    JProgressBar    JRadioButton    JScrollPane    JSlider    JSpinner    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.