Frames, Windows, and Panes


Frames , Windows , and Panes

In Java, you must place components such as buttons and text fields in a container. Applications, including applets with a GUI, use at least one container. When a GUI application runs, it typically displays its interface inside a frame. The following list describes three top-level windows that are closely related :

  • JWindow ” A window without a title bar, sizing borders, or Minimize/Maximize/Close buttons.

  • JFrame ” This container is also a window without a title bar, sizing borders, or Minimize/Maximize/Close buttons.

  • JDialog ” A special-purpose window, such as JOption.

The following hierarchy of the top-level windows shows the relationship between these components:

 java.lang.Object -java.awt.Component --java.awt.Container ---java.awt.Window ----javax.swing.JWindow ---java.awt.Window ----java.awt.Frame -----javax.swing.JFrame ---java.awt.Window ----java.awt.Dialog -----javax.swing.JDialog 

You can use these three containers to create your assignment's GUI. Keep in mind that a JWindow is a naked JFrame, and a JDialog is a single-purpose JWindow.

graphics/note_icon.gif

A JFrame holds several panes, so adding a component requires you to specify a pane, which becomes a content pane when it holds child components.


The following code snippet is an example of creating a frame:

 // Create frame JFrame myFrame = new JFrame("my frame"); // Create a button JComponent button = new JButton(); // Add button to frame's content pane; myFrame.getContentPane().add(button, BorderLayout.NORTH); myFrame.setVisible(true); 

Panes are related to frames in that they are windows, but they must be added to one of the top-level windows listed previously or to another pane. The following are the intermediate Swing containers:

  • JPanel ” This is a bare container you can add borders to.

  • JScrollPane ” This container presents a scrollable view of a component.

  • JSplitPane ” This container displays two components, left to right or top to bottom.

  • JToolBar ” This container groups components, often buttons, into a row or column.

  • JTabbedPane ” This container overlaps panels that are selected by tabbing.



JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 187

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net