Note that creating a program with a GUI is quite a bit more involved than simply creating a command line based program. With the GUI, you'll have to (or get to) learn about Frames, and components, and layout managers, and events, and graphics. With a command line based program, this could just be a quick and simple input process, some quick math, and then a simple text output.
If you're sure that you'd like to create a GUI, let's begin to walk that path and just create a very simple program that says "hello" from a graphical user interface. In the end, I'm imagining a program with either one or multiple TextFields, and a Button or two, and an area to display results.
Probably the simplest way to accomplish this is to construct a plain
Frame with the
word "hello" displayed on it.
<SideNote>
In case you haven't discovered it yet, the documentation for the standard Java API is a very useful tool when figuring out what does what in Java. This documentation is located online at
http://java.sun.com/j2se/1.4/docs/api/index.html and can also be downloaded from
http://java.sun.com/j2se/1.4/download.html - the link for the documentation is towards the bottom of the page.
Another very useful set of documents from Sun, is Sun's Java Tutorial at
http://java.sun.com/docs/books/tutorial/index.html .
</SideNote>
This displays a blank white frame:
What we've done here is create a very basic Java program that can be invoked in this fashion:
java Whatever after it has been successfully compiled. This program creates an instance of a Frame and displays it.
It's a rather annoying program, because the Frame cannot be directly closed using the 'X' in the upper right hand corner. You have to kill it through the console that created it, or, if that console is suppressed by some
IDE, then perhaps you'd have to go so far as to press Ctrl+Alt+Delete and kill the appropriate Java process.
Here's a quick solution to allow the Frame to be closed:
Now, the program has the ability to close the Frame. We can discuss the details of how this works later on (it involves event handling).
It would be arguably easier to manipulate the Frame, if the main program class itself were a Frame (by extending the Frame class) rather than just creating an instance of it in our program. Are you already mildly familiar with inheritance in Java?
Let's adjust our class to be a Frame and create and display an instance of itself:
By extending the class Frame, our class inherits all of the Frame's functionality and then allows us to easily use, adjust, and add to that functionality. Right now, I'd like to override the
paint method of the Frame in order to customize the display of our program. Are you familiar with the term
override?
This is what I'm suggesting:
When compiled and run, this program should now display a white Frame with the word "hello" displayed inside of it.
This
paint method can be used for basic displaying of graphics (another subject that we could later explore and discuss).
If this would seem to be the path that you'd like to walk, then just say so and I'd be glad to help you figure these concepts out and you'd do well to take a look at the documentation for the various classes that I've used in these examples (remember the link above?). Otherwise, I would not be surprised if the instructor is expecting that you simply create a command line based program - that's what many instructors expect during the beginning of learning to program in Java.
What'll it be pardner?