Sie sind auf Seite 1von 6

Java programming language class library provides a user interface toolkit called the Abstract Windowing Toolkit, or the

AWT. The AWT is , with proper guidance, the creation of a graphical user interface using the AWT is not only possible, but relatively straightforward.

user interface
The user interface is that part of a program that interacts with the user of the program. User interfaces take many forms. These forms range in complexity from simple command-line interfaces to the point-and-click graphical user interfaces provided by many modern applications.

Components and containers


A graphical user interface is built of graphical elements called components. Typical components include such items as buttons, scrollbars, and text fields. Components allow the user to interact with the program and provide the user with visual feedback about the state of the program. In the AWT, all user interface components are instances of class Component or one of its subtypes. Components do not stand alone, but rather are found within containers. Containers contain and control the layout of components. Containers are themselves components, and can thus be placed inside other containers. In the AWT, all containers are instances of class Container or one of its subtypes. Spatially, components must fit completely within the container that contains them. This nesting of components (including containers) into containers creates a tree of elements, starting with the container at the root of the tree and expanding out to the leaves, which are components such as buttons. The illustration in Figure 1 depicts a simple graphical user interface as it would look when displayed under Windows 95. Figure 2 shows the interface components from Figure 1 arranged as a tree.

Figure 1. A simple graphical user interface

Figure 2. User interface component tree

Types of components

Figure 3 shows the inheritance relationship between the user interface component classes provided by the AWT. Class Component defines the interface to which all components must adhere.

Figure 3. The inheritance relationship

Types of containers
The AWT provides four container classes. They are class Window and its two subtypes -- class Frame and class Dialog -- as well as the Panel class. In addition to the containers provided by the AWT, the Applet class is a container -- it is a subtype of the Panel class and can therefore hold components. Brief descriptions of each container class provided by the AWT are provided below.

A top-level display surface (a window). An instance of the Window class is not Window attached to nor embedded within another container. An instance of the Window class has no border and no title. A top-level display surface (a window) with a border and title. An instance of the Frame Frame class may have a menu bar. It is otherwise very much like an instance of the Window class. A top-level display surface (a window) with a border and title. An instance of the Dialog Dialog class cannot exist without an associated instance of the Frame class.

Panel

A generic container for holding components. An instance of the Panel class provides a container to which to add components.

Creating a container
Before adding the components that make up a user interface, the programmer must create a container. When building an application, the programmer must first create an instance of class Window or class Frame. When building an applet, a frame (the browser window) already exists. Since the Applet class is a subtype of the Panel class, the programmer can add the components to the instance of the Applet class itself. SWING For using swing we HAVE TO IMPORT SWING PACKAGE Swing was developed to provide a more sophisticated set of GUI components than the earlier Abstract Window Toolkit (AWT). Swing provides a native look and feel that emulates the look and feel of several platforms, and also supports a pluggable look and feel that allows applications to have a look and feel unrelated to the underlying platform. It has more powerful and flexible components than AWT. In addition to familiar components such as buttons, check boxes and labels, Swing provides several advanced components such as tabbed panel, scroll panes, trees, tables, and lists. Unlike AWT components, Swing components are not implemented by platform-specific code. Instead they are written entirely in Java and therefore are platform-independent. The term "lightweight" is used to describe such an element. Swing is a platform-independent, Model-View-Controller GUI framework for Java, which follows a single-threaded programming model.[4] Additionally, this framework provides a layer of abstraction between the code structure and graphic presentation of a Swing-based GUI.

Foundations[edit]
Swing is platform-independent because it is completely written in Java. Complete documentation for all Swing classes can be found in the Java API Guide.
Extensible

Swing is a highly modular-based architecture, which allows for the "plugging" of various custom implementations of specified framework interfaces: Users can provide their own custom implementation(s) of these components to override the default implementations using Java's inheritance mechanism.[5]

Swing is a component-based framework, whose components are all ultimately derived from the javax.swing.JComponent class. Swing objects asynchronously fire events, have bound properties, and respond to a documented set of methods specific to the component. Swing components are Java Beans components, compliant with the Java Beans Component Architecture specifications.
Customizable Given the programmatic rendering model of the Swing framework, fine control over the details of rendering of a component is possible. As a general pattern, the visual representation of a Swing component is a composition of a standard set of elements, such as a border, inset, decorations, and other properties. Typically, users will programmatically customize a standard Swing component (such as a JTable) by assigning specific borders, colors, backgrounds, opacities, etc. The core component will then use these properties to render itself. However, it is also completely possible to create unique GUI controls with highly customized visual representation. // a typical program // cliclking combobox and displaying details on label import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.JButton; import javax.swing.JComboBox;

public class first1 extends JFrame implements ActionListener { JLabel tb; JComboBox jc; public static void main(String h[]) { first1 he=new first1(); } first1() { Container contentpane=this.getContentPane();// Containe class contentpane.setLayout(new FlowLayout());//Flowlayout java.awt // in java.awt tb=new JLabel("bell"); add(tb); String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; jc= new JComboBox(petStrings);

jc.addActionListener(this); add(jc); setSize(300, 200); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible( true); } public void actionPerformed(ActionEvent e) { tb.setText((String)jc.getSelectedItem()); } }