| Beginning Swing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
To begin
your Swing program, lay out the program using stepwise refinement, draw
a picture of your GUI design, and list the components you want to use.
You'll then need to create an instance of a window or frame object
the preferable way to do so at this point is to extend JFrame and create
your own class. You will also need to include the ability for your
program to know when the user clicks the close box on the frame, to
end the program. Do this as follows:
class SomeFrame extends JFrame
{
public SomeFrame()
{
// set the size, location, etc.
setDefaultCloseOperation( EXIT_ON_CLOSE );
// add more stuff here...
setVisible(true);
}
My suggestion
is to start small begin by only writing the constructor to set
the size and location of the frame object, and add the close operation.
Then write main(), and create an instance of the frame. Compile
and run the program, to make sure the frame shows up on screen,
and that it closes when the user clicks the close box. Once you've
got that structure down, you can go back to the line above that says
"add more stuff..." and start to do so. If you add elements
one at a time, then compile and check them, you can find/figure out
errors much more easily than if you write large chunks of code and
don't know where the problem might lie. Get your GUI created and working,
remember to add the listeners you need for buttons, etc., and afterwards
write the methods you need to use to respond to the listeners and
to process any input or print results.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
LAYOUT
MANAGERS
These three are the more common choices for layout managers, although they are not the only ones available (the others tend to be somewhat more complicated to implement):
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
USEFUL CLASSES AND METHODS In the tables below, I've pulled out several classes and methods that you may find useful. There are many more than I've listed all objects have no-argument constructors as well as those below, and I've only put the constructors with arguments which are most often used. The first column contains the return type for the method, if it returns an object or value. The second column is the name of the method and its parameters, and the third column is a brief description. You can find additional information in the lecture notes, the chapters in the Savitch textbook, the Resources Page, or any Java reference book that includes Swing. I've included brief info on superclasses, since there are many methods in the superclasses which are not listed below nor in the separate class listings. Remember that these are only a few of the available Swing methods and classes. For more details and documentation, go to Sun's web site at http://java.sun.com/j2se/1.5.0/docs/api/index.html. For each component you wish to use, remember to give it a variable name and create an instance of it as an object (using the keyword new) - then you can use that name and the 'dot' operator to access the above methods. You will need to add() each component to your panel, or use the content pane to add() it to the JFrame (or subclass object). If you use JButtons, make sure you create and add the listener objects to them, and write actionPerformed() to handle the events when the user clicks the button. You will need to either write an inner class which extends ActionListener and which defines actionPerformed(), or you may set up your JFrame subclass to implement ActionListener, in which case you can simply write actionPerformed() as another method in the class, and pass the instance being created to the method as its own listener, using "this". |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ActionListeners
When you use a JButton, you will need to set up ActionListeners to be able to respond to user events. Sometimes you may have more than one button, and need to determine which one the user clicked. In order to do so, you must have a class that implements the ActionListener interface and defines its required method, public void actionPerformed(ActionEvent ae)
.
The secret to doing this is the ActionEvent object which comes in as a
parameter, created by the JVM when the listener is notified of the event.
The ActionEvent object has a few useful methods that you may call to get
information about what has occurred. Among other things, the ActionEvent knows the source object which caused the event. It can be accessed using the ActionEvent parameter's name with getSource()
,
and it returns the reference (memory address) of the object producing
the event. This allows for direct comparison of references with the
JButtons or other objects in your program. This is generally the preferred
approach.There is also a value named actionCommand which is associated with that source. The actionCommand for a Button object is accessed through the method getActionCommand()
,
and returns (by default) the object's text value as a String.
In other words, if your JButton's text says "Press me!", that
is what will be returned by the method. However, when your button's text
is more lengthy, you can also set the JButton's actionCommand when
you create it using the JButton method
setActionCommand(String value)
,
and if it is set, that String will be returned instead of the actual
JButton text. Either of these can be compared to your various buttons'
text (or setActionCommand values) using String methods.When you write your listener class and define actionPerformed(), you will need to use these methods to find out about the event, so that you can make an appropriate response. If your response is very simple, you may write it within the actionPerformed() method; if it is more detailed or complicated, feel free to write and call other methods in the class to keep your code readable. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||