|
CIT 594: Forté Tutorial II (Creating an
application) |
In this tutorial we create an application named Shuffler. Shuffler is an application that displays a window containing a text field and a button. Initially, the text field contains the numbers 1 through 20, in order, but each time the "Shuffle" button is clicked, the numbers are rearranged randomly.
Shuffler to hold this tutorial project. File -> Mount Filesystem....
Add Local Directory is selected, and click the Browse...
button after the Directory: field. Find your newly
created folder in the Add Local Directory window, and select
it (but don't open it), so that its name is showing in the File
Name: field. Click OK.Shuffler) should appear in
the Mount Filesystem window. Click OK. New.
Or, if you closed this window earlier, just choose File -> New
on the main menu.Templates -> Swing
Forms -> JFrame. Read what it says about JFrames. Click Next>.Shuffler
folder. Type in Shuffler in the Name: field,
and click Finish. (These names don't have to be the same,
but they are in this example.) Shuffler,
you will notice that the JFrame has a BorderLayout manager.
JTextField (in the Swing tabbed pane) in
Forté's main control window. (Use the tool tips to find it.) Form [Shuffler] window to
place the text field. jTextField1 from the text field in the
Properties tabbed pane. The text field now becomes blank.Variable Name, and change its value
to outputField.
JPanel (in the Swing tabbed pane). Form [Shuffler] window, under
the text field.Variable
Name to buttonPanel. Form [Shuffler] window, under
the text field.
text to Shuffle and change its Variable Name
to shuffleButton.ScrambledArray.
We want it to have toString and scramble methods.
We could easily type these methods in later, but here we're going to let Forté
set them up for us.
File -> New from the main menu bar. Templates -> Classes
-> Class (and read what it says about this selection). Click
Next>. ScrambledArray as
the name, and make sure the Shuffler directory is selected.
Click Next>. Next>. Next>.
class Object, select toString(), and click Add>.
Click Next>. New. Type in
scramble for the Name:, and leave its
Return Type: as void. Click Finish.ScrambledArray the declaration:
int[] theArray = new int[20]; public class ScrambledArray {.
for (int i = 0; i < theArray.length; i++)
{
theArray[i] =
i + 1;
}
Notice how Forté automatically indents everything nicely for
you as you type. import java.util.*;
scramble() method:Random random = new Random();
for (int i = theArray.length - 1; i > 0; i--) {
int location = random.nextInt(i + 1);
int temp = theArray[i];
theArray[i] = theArray[location];
theArray[location] = temp;
}
If you did this by cutting and pasting, chances are your indentation got somewhat
messed up. Select the code that is badly indented, right click the mouse button,
and choose Reformat Code. (Isn't that nice?) java.lang.toString() method:
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < theArray.length; i++) {
buffer.append(theArray[i] + ", ");
}
buffer.setLength(buffer.length() - 2);
return buffer.toString();
Shuffler class (by double-clicking
on it in the Explorer [Filesystems] window. ScrambledArray numbers; as a new instance variable
(Just type it in.)main method, replace: new Shuffler().show();
with:
Shuffler shuffler = new Shuffler();
shuffler.init();
and add a new method, as follows:
private void init() {
show();
numbers = new ScrambledArray();
outputField.setText(numbers.toString());
}
The reason for doing things this way is to avoid "static poisoning," where
the compiler keeps complaining that you "can't reference a non-static
variable from a static context," and to solve it you have to keep making
one thing after another static. Instead, we just create an instance (shuffler)
and use an instance method of shuffler to do all the work.Shuffle button in your Form window.
This will add a shuffleButtonActionPerformed method to your
Shuffler class, and bring you to it so you can start adding
code.numbers.scramble(); outputField.setText(numbers.toString());
File -> Save All.
Shuffler class is selected (if the
wrong class is selected, you will get a NoSuchMethodError: main
message) and clicking on the green triangle in Forté's main control
window . If you get an error, double-click on the error message to jump to
that location in the file, and fix the error.
Notice, by the way, that executable classes are marked as such in the
Explorer [Filesystems] window with a tiny green triangle.
outputField. Set columns to 30.
Try the program again. Almost big enough? Try 32.File -> Save All.