Escolar Documentos
Profissional Documentos
Cultura Documentos
GUI
Sahirul Alim Tri Bawono
Overview
GUI
AWT
Frame
Component
Panel
Layout Manager
Event
GUI
With the availability of powerful and cheap hardware, and widespread and
diverse use of computers, easy and intuitive interfaces have become an
important aspect to developers.
There are two sets of Java APIs for graphics programming: AWT (Abstract
Windowing Toolkit) and Swing.
AWT API was introduced in JDK 1.0. Most of the AWT components have become
obsolete and should be replaced by newer Swing components.
Swing API, a much more comprehensive set of graphics libraries that enhances the
AWT, was introduced as part of Java Foundation Classes (JFC) after the release of
JDK 1.1. JFC consists of Swing, Java2D, Accessibility, Internationalization, and
Pluggable Look-and-Feel Support APIs. JFC was an add-on to JDK 1.1 but has been
integrated into core Java since JDK 1.2.
AWT
Consistent with the principles of abstraction, the AWT model for graphical
user-interfaces in Java is broken into several constituents with its own
concerns and functionality
Frame
Components
Panels
Layout managers
event
Frame
The frame abstraction allows for independent windows in the Java host.
While a Java application typically runs in a window, it may also create more
graphical windows to provide alternative or complementary views.
Frame
Component
The AWT components consist of a custom set of widgets for various styles of
user interaction. The range of widgets includes:
Text label
Button
Scrollbars
Component
Component
Panels
The AWT panels are used to contain a set of logically related AWT components. For example, user authentication by a user name and password may be
pre-sented by two text field components together with two buttons. One
button allows the user to proceed with authentication, while the other
cancels the request. These components may be logically included in an AWT
panel.
Panel
Layout manager
The layout manager constituent of the AWT model allows for layout control of
components within panels.
Two other layout schemes are commonly used: The GridLayout class implements a table-style layout with fixed row sand columns
If it is the only component in its container, use GridLayout or BorderLayout. Otherwise, BorderLayout or
GridBagLayout might be a good match.
If you use BorderLayout, you will need to put the space-hungry component in the center. With GridBagLayout, you
will need to set the constraints for the component so that fill=GridBagConstraints.BOTH. Another possibility is to
use BoxLayout, making the space-hungry component specify very large preferred and maximum sizes.
Scenario:You need to display a few components in a compact row at their natural size.
Scenario:You need to display a few components of the same size in rows and columns.
Scenario:You need to display aligned columns, as in a form-like interface where a column of labels is used
to describe text fields in an adjacent column.
Scenario:You need to display a few components in a row or column, possibly with varying amounts of space
between them, custom alignment, or custom component sizes.
Consider using a JPanel to group the components and using either the JPanel's default FlowLayout manager or the
BoxLayout manager. SpringLayout is also good for this.
SpringLayout is a natural choice for this. The SpringUtilities class used by several Tutorial examples defines a
makeCompactGrid method that lets you easily align multiple rows and columns of components.
Consider either using a very flexible layout manager such as GridBagLayout or SpringLayout, or grouping the
components into one or more JPanels to simplify layout. If you take the latter approach, each JPanel might use a
different layout manager.
Event
Currently, the views painted by the application frame with its constituent
components are like empty shells with no application processing logic
underneath. In a typical scenario, processing logic would involve:
retrieving the state of switches, such as to determine the states of the Save
config and Ignore colors Checkboxes, or reading input keyed into the top
TextArea; or
Event handling in JDK 1.1 adopts the delegation model and consists of event
sources and event listeners. One or more event listeners may register to be
notified of particular events with a source. Any object may be a listener by
implementing the appropriate EventListenerinterface. Just as in JDK 1.0,
where there are various groups of events, there are also various groups of
EventListeners in JDK 1.1.
The following code fragment Listing uses components as in previous examples, but using JDK 1.1styledevent handling. Corresponding to Button and
Checkbox components and the use of Frames, there are three groups of
listeners required. We need EventListeners for action, item, and window
events. The corresponding listeners are ActionListener, ItemListener,and
WindowListener, respectively.
Swing API
Overview
Swing Component
JRadioButton
JComboBox
The AWT components we have been discussing were the first graphical components Java
ever had. For easy implementation, they are ultimately mapped to the native graphical
widgets provided by the underlying windowing systems by various machine platforms
such as Windows, Linux, or Mac OS.
Thus, while AWT runs on different platform (just like all other Java codes), AWT
components look different on the various platforms. This is not too significant except
that different dimensions can sometimes drastically alter how designed interfaces look.
This shortcoming has led to the introduction of Swing components in JDK 1.2. Swing
components work in a similar fashion to AWT components in that we still instantiate
suitable component classes and attach handlers for relevant events.
The wonderful aspect of Swing components is that they are implemented in pure Java.
As such, they exhibit the same look-and-feel across platforms. Swing provides a rich set
of components for developing a responsive user-interface
Moving from AWT to Swing is generally easy. While the AWT component classes
such as Button, Label, Checkbox,and Frame are defined in the java.awt
package, the Swing package javax.swing contains classes JButton, JLabel,
JCheckBox and JFrame.
Swing Component
Swing is written in pure Java (except a few classes) and therefore is 100% portable.
Swing application uses AWT's layout manager (such as FlowLayout and BorderLayout in package java.awt).
Swing implements double-buffering and automatic repaint batching for smoother screen repaint
Swing introduces JLayeredPane and JInternalFrame for creating Multiple Document Interface (MDI)
applications.
JFrame: used for the application's main window (with an icon, a title,
minimize/maximize/close buttons, an optional menu-bar, and a
content-pane), as illustrated
BorderLayout divides the container into five areas, and you can add a
component to each area. The five regions correspond to the top, left,
bottom, and right sides of the container, along with one in the center, as
illustrated in Figure
BoxLayout
Example
JRadioButton
Example
Class pilihan
{
public static void main(String args[])
{
JRadioButton rb1,rb2;
JFrame f= new Jframe(Radioku);
Container pane = f.getContentPane();
pane.setLayout(new FlowLayout);
pane.add(rb1=new JRadioButton(Laki-laki));
pane.add(rb2=new JRadioButton(Perempuan));
ButtonGroup bg = new ButtonGroup();
bg.add( rb1);
bg.add(rb2);
f.setVisible(true);
}
}
ComboBox
A combo box (sometimes called a drop-down list) enables the user to select
one item from a list
Example
class pilihan
{
public static void main(String args[])
{
JRadioButton rb1,rb2;
JFrame f= new Jframe(Radioku);
Container pane = f.getContentPane();
pane.setLayout(new FlowLayout);
String[] data = {Laki-laki,Perempuan};
pane.add(new JComboBox(data));
f.setVisible(true);
}
}
Referensi
Poo, D., et. All., 2008,Object Oriented Programming And Java, SpringerVerlag. London.
Deitel, P., et. All., 2005, Java How to Program, Prentice Hall, New York.