|
Swing Help |
|
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 add elements for 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. For the present, use the method setDefaultCloseOperation(), inherited from JFrame, with the JFrame final variables:
class SomeFrame extends JFrame
{
public SomeFrame()
{
// set the title, size, location, etc.
// add more stuff here...
setDefaultCloseOperation( EXIT_ON_CLOSE );
setVisible(true);
}
}
Our suggestion is to start small begin by only writing the constructor to set the size and location of the frame object, and to make the program end. Then write main, and create an instance of the frame. Compile and run the program, to make sure the frame will show up on screen, and that it will close 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 blocks of code and don't know where the problem might lie. Get your GUI created and working, remember to add the listener(s) you need for buttons, etc., and afterwards write the methods you need to use to respond to the listener(s) and to process any input or print results. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
LAYOUT
MANAGERS
These are the more common options 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 you may wish to use. 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 either the lecture notes, the Savitch chapters in the coursepack, 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 the Java API's at http://download.oracle.com/javase/6/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 want to access
components outside the constructor (such as in actionPerformed() ), remember
to make them private instance variables in the class. 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 class to extend JFrame AND to implement ActionListener, in which case you can simply write actionPerformed() as another method in the class. You will then pass the instance being created to the method as its own listener, using "this". More on this technique in section... |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ActionListeners When you use a JButton, you will need to set up an ActionListener 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, as well as a value named actionCommand which is associated with that source. The first of these 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 comparison of references with the JButtons
in your program. This is generally the preferred approach.The actionCommand for a Button object is accessed through the method getActionCommand(),
and returns (by default) the text value set on the object 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. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||