Your goal this week is to expand on your understanding of the Swing API as well as to hammer home the fundamental tools for working with Java objects.
Here's a small screenshot of what your new program might look like:
More explicitly, you must provide:
Sounds easy enough, right?
new
statement.
implements
an interface.
ActionListener
with a component to be able to listen for mouse clicks.
main
method
Lab4
.
main
method for the Lab4
class. Instead, create an instance of
Lab4
inside the main
method and put the
functionality inside non-static
methods of Lab4
.
This is good practice, because it means that you can create multiple
instances of Lab4
; this isn't really important here but in
general it's very important, so it's a good habit to get into. In addition,
don't use static
data unless there is a good reason to.
Lab4
inherit from any other class (e.g.
GUI (Graphical User Interface) classes); instead, make the GUI classes you
need fields of Lab4
and initialize them in your constructor.
This is called "composition"; we say the Lab4
class "has-a" GUI
object. Inheritance implies that the Lab4
class "is-a" GUI
object, which is the wrong way to think about it.
BorderLayout
as your
JFrame
's content pane's layout manager. You can add a
borderless, invisible JPanel
container object to the
NORTH
of the content pane and your special "step" button to
the SOUTH
. You can then set a different, more applicable layout
manager on your inner JPanel
and add your grid of 100 buttons to
it. What layout manager, you ask? Hint: you are Laying Out a Grid...
WindowListener
to the main frame which calls the
exit
function when the close button of the window is clicked.
There are other ways to make the close button work but this is good practice
in using Listener classes. Use an anonymous inner class to create the
WindowListener
. NOTE: It's OK (in fact it's advisable)
to use a WindowAdapter
instead of a
WindowListener
. A WindowAdapter
is an
implementation of WindowListener
with empty methods for each
method. If you make an anonymous inner class from WindowAdapter
and add the methods you want to implement, you don't have to supply the other
methods.
Lab4
class create a two-dimensional array of your button
objects.
final
, which is unnecessarily
restrictive anyway. For this assignment, you can do everything you need to
do inside anonymous inner classes by calling methods.
n
, as a command line parameter. Create
an n
by n
grid instead of a fixed, 10 by 10 one.