Java Reference
In-Depth Information
2.
If graphics are used, make an extra class that extends
JPanel
. That
class must provide a
paintComponent
method and a public method that
can be used by the container to communicate to it. It may also need to
provide a constructor.
3.
Pick a layout and issue a
setLayout
command.
4.
Add components to the GUI using
add
.
5.
Handle events. The simplest way to do this is to use a
JButton
and trap
the button push with
actionPerformed
.
Once a GUI class is written, an application defines a class that extends
CloseableFrame
with a
main
routine. The
main
routine simply creates an
instance of this extended frame class, places the GUI panel inside the frame's
content pane, and issues a
pack
command and a
show
command for the frame.
know about swing?
What we have described so far will work well for toy user interfaces and is an
improvement over console-based applications. But there are significant com-
plications that a professional applications programmer would have to deal
with.
It is rare that the layout manager will make you happy. Often you need to
tinker by adding additional subpanels. To help out, Swing defines elements
such as spacers, struts, and so on that allow you to position elements more
precisely, along with elaborate layout managers. Using these elements is quite
challenging.
Other Swing components include sliders, progress bars, scrolling (which
can be added to any
JComponent
), password textfields, file choosers, option
panes and dialog boxes, tree structures (such as what you see in File Manager
on Windows systems), tables, and on and on. Image acquisition and display is
also supported by Swing. Additionally, one often needs to know about fonts,
colors, and the screen environment that one is working in.
Additionally, there is the important issue of what happens if an event
occurs while you are in an event handler. It turns out that events are queued.
However, if you get trapped in an event handler for a long time, your applica-
tion can appear unresponsive; we've all seen this in application code. For
instance, if the button-handling code has an infinite loop, you will not be able
to close a window. To solve this problem, typically programmers use a tech-
nique known as
multithreading
, which opens up a whole new can of worms.
Search WWH ::
Custom Search