Académique Documents
Professionnel Documents
Culture Documents
The simplest way to create a graphical interface in java is using the “option pane” pop up.
JOptionPane can be used in three major ways: to display a message, to present a list of choice
to the user and to ask to the user to type input. (ShowMessageDialog, showConfirmDialog,
showInputDialog).
One limitation one showConfirmDialog always returns the user’s input as a String.
An onscreen window is called a frame. The graphical widgets inside a frame, such as buttons
or text inputs fields are collectively called components. The frame forms the physical windows
that are seen on the screen.
Frames are represented as objects of the JFrame class. You can display it on the screen by
calling its setVisible method and passing it Boolean value true.
Our empty frames are not very interesting. Buttons are represented by the JButton class. Each
JButton object represent one button on the screen.
A java object that determines the positions, sizes, and resizing behavior of the components
within a frame or other container on the screen are called Layout Manager. To position the
components in a diferent way, we must set a new layout manager for the frame and use it to
position the components.
Handling an event:
In order to create useful interactive GUI’s, you must learn how to handle Java events. Java’s
GUI system creates a special kind of object called event to represent this action.
Event:
An object that represents a user’s interaction with a GUI component and that can be handled
by your programs to create interactive components. By default, if you don’t specify how to
react to an event, it goes unnoticed by your program.
You can cause the program to respond to a particular event by using an object called a listener.
Listener: An object that is notified when an event occurs and attach it to the component of
interest.
To handle an event, create a listener object and attach it to the component of interest. The
listener object contains the code you want to run when the appropriate event occurs.
A panel is a container is an onscreen container component that acts solely as a container for
other components and generally cannot be seen. A panel has its own layout manager that
controls position and sizes of any component.
You can create a JPanel with no parameters or can specify the layout manager to use. Once
you’ve constructed the panel, you can add components to it using its add method.
A common strategy is to use a BorderLayout for the frame, then add panels to some or all its
five regions.
We can put a BorderLayou in the Center and a GridLayout in the South for example.
(Telephone and MailMessage example).
BMI GUI example, in this program we want to calculate the Body Mass Index.